preproc: always reprocess smacros after %+ - pasting
[nasm/avx512.git] / output / outelf32.c
blob7159a1f16dc8b18fbea8178eb9957376ba4c9478
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * outelf32.c output routines for the Netwide Assembler to produce
36 * ELF32 (i386 of course) object file format
39 #include "compiler.h"
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <inttypes.h>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "saa.h"
50 #include "raa.h"
51 #include "stdscan.h"
52 #include "output/outform.h"
53 #include "output/outlib.h"
54 #include "rbtree.h"
56 #include "output/elf32.h"
57 #include "output/dwarf.h"
58 #include "output/outelf.h"
60 #ifdef OF_ELF32
63 * Relocation types.
65 struct Reloc {
66 struct Reloc *next;
67 int32_t address; /* relative to _start_ of section */
68 int32_t symbol; /* symbol index */
69 int type; /* type of relocation */
72 struct Symbol {
73 struct rbtree symv; /* symbol value and symbol rbtree */
74 int32_t strpos; /* string table position of name */
75 int32_t section; /* section ID of the symbol */
76 int type; /* symbol type */
77 int other; /* symbol visibility */
78 int32_t size; /* size of symbol */
79 int32_t globnum; /* symbol table offset if global */
80 struct Symbol *nextfwd; /* list of unresolved-size symbols */
81 char *name; /* used temporarily if in above list */
84 struct Section {
85 struct SAA *data;
86 uint32_t len, size, nrelocs;
87 int32_t index;
88 int type; /* SHT_PROGBITS or SHT_NOBITS */
89 uint32_t align; /* alignment: power of two */
90 uint32_t flags; /* section flags */
91 char *name;
92 struct SAA *rel;
93 int32_t rellen;
94 struct Reloc *head, **tail;
95 struct rbtree *gsyms; /* global symbols in section */
98 #define SECT_DELTA 32
99 static struct Section **sects;
100 static int nsects, sectlen;
102 #define SHSTR_DELTA 256
103 static char *shstrtab;
104 static int shstrtablen, shstrtabsize;
106 static struct SAA *syms;
107 static uint32_t nlocals, nglobs, ndebugs; /* Symbol counts */
109 static int32_t def_seg;
111 static struct RAA *bsym;
113 static struct SAA *strs;
114 static uint32_t strslen;
116 static FILE *elffp;
117 static efunc error;
118 static evalfunc evaluate;
120 static struct Symbol *fwds;
122 static char elf_module[FILENAME_MAX];
124 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
125 static uint8_t elf_abiver = 0; /* Current ABI version */
127 extern struct ofmt of_elf32;
128 extern struct ofmt of_elf;
130 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
132 static struct ELF_SECTDATA {
133 void *data;
134 int32_t len;
135 bool is_saa;
136 } *elf_sects;
137 static int elf_nsect, nsections;
138 static int32_t elf_foffs;
140 static void elf_write(void);
141 static void elf_sect_write(struct Section *, const uint8_t *,
142 uint32_t);
143 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
144 int, int);
145 static void elf_write_sections(void);
146 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
147 static struct SAA *elf_build_reltab(int32_t *, struct Reloc *);
148 static void add_sectname(char *, char *);
150 struct stabentry {
151 uint32_t n_strx;
152 uint8_t n_type;
153 uint8_t n_other;
154 uint16_t n_desc;
155 uint32_t n_value;
158 struct erel {
159 int offset, info;
162 struct symlininfo {
163 int offset;
164 int section; /* section index */
165 char *name; /* shallow-copied pointer of section name */
168 struct linelist {
169 struct symlininfo info;
170 int line;
171 char *filename;
172 struct linelist *next;
173 struct linelist *last;
176 struct sectlist {
177 struct SAA *psaa;
178 int section;
179 int line;
180 int offset;
181 int file;
182 struct sectlist *next;
183 struct sectlist *last;
186 /* common debug variables */
187 static int currentline = 1;
188 static int debug_immcall = 0;
190 /* stabs debug variables */
191 static struct linelist *stabslines = 0;
192 static int numlinestabs = 0;
193 static char *stabs_filename = 0;
194 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
195 static int stablen, stabstrlen, stabrellen;
197 /* dwarf debug variables */
198 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
199 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
200 static int dwarf_numfiles = 0, dwarf_nsections;
201 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
202 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
203 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
204 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
205 abbrevlen, linelen, linerellen, framelen, loclen;
206 static int32_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
208 static struct dfmt df_dwarf;
209 static struct dfmt df_stabs;
210 static struct Symbol *lastsym;
212 /* common debugging routines */
213 static void debug32_typevalue(int32_t);
214 static void debug32_deflabel(char *, int32_t, int64_t, int, char *);
215 static void debug32_directive(const char *, const char *);
217 /* stabs debugging routines */
218 static void stabs32_linenum(const char *filename, int32_t linenumber, int32_t);
219 static void stabs32_output(int, void *);
220 static void stabs32_generate(void);
221 static void stabs32_cleanup(void);
223 /* dwarf debugging routines */
224 static void dwarf32_init(struct ofmt *, void *, FILE *, efunc);
225 static void dwarf32_linenum(const char *filename, int32_t linenumber, int32_t);
226 static void dwarf32_output(int, void *);
227 static void dwarf32_generate(void);
228 static void dwarf32_cleanup(void);
229 static void dwarf32_findfile(const char *);
230 static void dwarf32_findsect(const int);
233 * Special NASM section numbers which are used to define ELF special
234 * symbols, which can be used with WRT to provide PIC and TLS
235 * relocation types.
237 static int32_t elf_gotpc_sect, elf_gotoff_sect;
238 static int32_t elf_got_sect, elf_plt_sect;
239 static int32_t elf_sym_sect, elf_tlsie_sect;
241 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
243 elffp = fp;
244 error = errfunc;
245 evaluate = eval;
246 (void)ldef; /* placate optimisers */
247 sects = NULL;
248 nsects = sectlen = 0;
249 syms = saa_init((int32_t)sizeof(struct Symbol));
250 nlocals = nglobs = ndebugs = 0;
251 bsym = raa_init();
252 strs = saa_init(1L);
253 saa_wbytes(strs, "\0", 1L);
254 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
255 strslen = 2 + strlen(elf_module);
256 shstrtab = NULL;
257 shstrtablen = shstrtabsize = 0;;
258 add_sectname("", "");
260 fwds = NULL;
262 elf_gotpc_sect = seg_alloc();
263 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf32,
264 error);
265 elf_gotoff_sect = seg_alloc();
266 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf32,
267 error);
268 elf_got_sect = seg_alloc();
269 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf32,
270 error);
271 elf_plt_sect = seg_alloc();
272 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf32,
273 error);
274 elf_sym_sect = seg_alloc();
275 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf32,
276 error);
277 elf_tlsie_sect = seg_alloc();
278 ldef("..tlsie", elf_tlsie_sect + 1, 0L, NULL, false, false, &of_elf32,
279 error);
281 def_seg = seg_alloc();
284 static void elf_init_hack(FILE * fp, efunc errfunc, ldfunc ldef,
285 evalfunc eval)
287 of_elf32.current_dfmt = of_elf.current_dfmt; /* Sync debugging format */
288 elf_init(fp, errfunc, ldef, eval);
291 static void elf_cleanup(int debuginfo)
293 struct Reloc *r;
294 int i;
296 (void)debuginfo;
298 elf_write();
299 fclose(elffp);
300 for (i = 0; i < nsects; i++) {
301 if (sects[i]->type != SHT_NOBITS)
302 saa_free(sects[i]->data);
303 if (sects[i]->head)
304 saa_free(sects[i]->rel);
305 while (sects[i]->head) {
306 r = sects[i]->head;
307 sects[i]->head = sects[i]->head->next;
308 nasm_free(r);
311 nasm_free(sects);
312 saa_free(syms);
313 raa_free(bsym);
314 saa_free(strs);
315 if (of_elf32.current_dfmt) {
316 of_elf32.current_dfmt->cleanup();
320 static void add_sectname(char *firsthalf, char *secondhalf)
322 int len = strlen(firsthalf) + strlen(secondhalf);
323 while (shstrtablen + len + 1 > shstrtabsize)
324 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
325 strcpy(shstrtab + shstrtablen, firsthalf);
326 strcat(shstrtab + shstrtablen, secondhalf);
327 shstrtablen += len + 1;
330 static int elf_make_section(char *name, int type, int flags, int align)
332 struct Section *s;
334 s = nasm_malloc(sizeof(*s));
336 if (type != SHT_NOBITS)
337 s->data = saa_init(1L);
338 s->head = NULL;
339 s->tail = &s->head;
340 s->len = s->size = 0;
341 s->nrelocs = 0;
342 if (!strcmp(name, ".text"))
343 s->index = def_seg;
344 else
345 s->index = seg_alloc();
346 add_sectname("", name);
347 s->name = nasm_malloc(1 + strlen(name));
348 strcpy(s->name, name);
349 s->type = type;
350 s->flags = flags;
351 s->align = align;
352 s->gsyms = NULL;
354 if (nsects >= sectlen)
355 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
356 sects[nsects++] = s;
358 return nsects - 1;
362 static int32_t elf_section_names(char *name, int pass, int *bits)
364 char *p;
365 uint32_t flags, flags_and, flags_or;
366 uint32_t align;
367 int type, i;
370 * Default is 32 bits.
372 if (!name) {
373 *bits = 32;
374 return def_seg;
377 p = name;
378 while (*p && !nasm_isspace(*p))
379 p++;
380 if (*p)
381 *p++ = '\0';
382 flags_and = flags_or = type = align = 0;
384 while (*p && nasm_isspace(*p))
385 p++;
386 while (*p) {
387 char *q = p;
388 while (*p && !nasm_isspace(*p))
389 p++;
390 if (*p)
391 *p++ = '\0';
392 while (*p && nasm_isspace(*p))
393 p++;
395 if (!nasm_strnicmp(q, "align=", 6)) {
396 align = atoi(q + 6);
397 if (align == 0)
398 align = 1;
399 if ((align - 1) & align) { /* means it's not a power of two */
400 error(ERR_NONFATAL, "section alignment %d is not"
401 " a power of two", align);
402 align = 1;
404 } else if (!nasm_stricmp(q, "alloc")) {
405 flags_and |= SHF_ALLOC;
406 flags_or |= SHF_ALLOC;
407 } else if (!nasm_stricmp(q, "noalloc")) {
408 flags_and |= SHF_ALLOC;
409 flags_or &= ~SHF_ALLOC;
410 } else if (!nasm_stricmp(q, "exec")) {
411 flags_and |= SHF_EXECINSTR;
412 flags_or |= SHF_EXECINSTR;
413 } else if (!nasm_stricmp(q, "noexec")) {
414 flags_and |= SHF_EXECINSTR;
415 flags_or &= ~SHF_EXECINSTR;
416 } else if (!nasm_stricmp(q, "write")) {
417 flags_and |= SHF_WRITE;
418 flags_or |= SHF_WRITE;
419 } else if (!nasm_stricmp(q, "tls")) {
420 flags_and |= SHF_TLS;
421 flags_or |= SHF_TLS;
422 } else if (!nasm_stricmp(q, "nowrite")) {
423 flags_and |= SHF_WRITE;
424 flags_or &= ~SHF_WRITE;
425 } else if (!nasm_stricmp(q, "progbits")) {
426 type = SHT_PROGBITS;
427 } else if (!nasm_stricmp(q, "nobits")) {
428 type = SHT_NOBITS;
429 } else if (pass == 1) {
430 error(ERR_WARNING, "Unknown section attribute '%s' ignored on"
431 " declaration of section `%s'", q, name);
435 if (!strcmp(name, ".shstrtab") ||
436 !strcmp(name, ".symtab") ||
437 !strcmp(name, ".strtab")) {
438 error(ERR_NONFATAL, "attempt to redefine reserved section"
439 "name `%s'", name);
440 return NO_SEG;
443 for (i = 0; i < nsects; i++)
444 if (!strcmp(name, sects[i]->name))
445 break;
446 if (i == nsects) {
447 const struct elf_known_section *ks = elf_known_sections;
449 while (ks->name) {
450 if (!strcmp(name, ks->name))
451 break;
452 ks++;
455 type = type ? type : ks->type;
456 align = align ? align : ks->align;
457 flags = (ks->flags & ~flags_and) | flags_or;
459 i = elf_make_section(name, type, flags, align);
460 } else if (pass == 1) {
461 if ((type && sects[i]->type != type)
462 || (align && sects[i]->align != align)
463 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
464 error(ERR_WARNING, "section attributes ignored on"
465 " redeclaration of section `%s'", name);
468 return sects[i]->index;
471 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
472 int is_global, char *special)
474 int pos = strslen;
475 struct Symbol *sym;
476 bool special_used = false;
478 #if defined(DEBUG) && DEBUG>2
479 fprintf(stderr,
480 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
481 name, segment, offset, is_global, special);
482 #endif
483 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
485 * This is a NASM special symbol. We never allow it into
486 * the ELF symbol table, even if it's a valid one. If it
487 * _isn't_ a valid one, we should barf immediately.
489 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
490 strcmp(name, "..got") && strcmp(name, "..plt") &&
491 strcmp(name, "..sym") && strcmp(name, "..tlsie"))
492 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
493 return;
496 if (is_global == 3) {
497 struct Symbol **s;
499 * Fix up a forward-reference symbol size from the first
500 * pass.
502 for (s = &fwds; *s; s = &(*s)->nextfwd)
503 if (!strcmp((*s)->name, name)) {
504 struct tokenval tokval;
505 expr *e;
506 char *p = special;
508 while (*p && !nasm_isspace(*p))
509 p++;
510 while (*p && nasm_isspace(*p))
511 p++;
512 stdscan_reset();
513 stdscan_bufptr = p;
514 tokval.t_type = TOKEN_INVALID;
515 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
516 if (e) {
517 if (!is_simple(e))
518 error(ERR_NONFATAL, "cannot use relocatable"
519 " expression as symbol size");
520 else
521 (*s)->size = reloc_value(e);
525 * Remove it from the list of unresolved sizes.
527 nasm_free((*s)->name);
528 *s = (*s)->nextfwd;
529 return;
531 return; /* it wasn't an important one */
534 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
535 strslen += 1 + strlen(name);
537 lastsym = sym = saa_wstruct(syms);
539 memset(&sym->symv, 0, sizeof(struct rbtree));
541 sym->strpos = pos;
542 sym->type = is_global ? SYM_GLOBAL : 0;
543 sym->other = STV_DEFAULT;
544 sym->size = 0;
545 if (segment == NO_SEG)
546 sym->section = SHN_ABS;
547 else {
548 int i;
549 sym->section = SHN_UNDEF;
550 if (nsects == 0 && segment == def_seg) {
551 int tempint;
552 if (segment != elf_section_names(".text", 2, &tempint))
553 error(ERR_PANIC,
554 "strange segment conditions in ELF driver");
555 sym->section = nsects;
556 } else {
557 for (i = 0; i < nsects; i++)
558 if (segment == sects[i]->index) {
559 sym->section = i + 1;
560 break;
565 if (is_global == 2) {
566 sym->size = offset;
567 sym->symv.key = 0;
568 sym->section = SHN_COMMON;
570 * We have a common variable. Check the special text to see
571 * if it's a valid number and power of two; if so, store it
572 * as the alignment for the common variable.
574 if (special) {
575 bool err;
576 sym->symv.key = readnum(special, &err);
577 if (err)
578 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
579 " valid number", special);
580 else if ((sym->symv.key | (sym->symv.key - 1))
581 != 2 * sym->symv.key - 1)
582 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
583 " power of two", special);
585 special_used = true;
586 } else
587 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
589 if (sym->type == SYM_GLOBAL) {
591 * If sym->section == SHN_ABS, then the first line of the
592 * else section would cause a core dump, because its a reference
593 * beyond the end of the section array.
594 * This behaviour is exhibited by this code:
595 * GLOBAL crash_nasm
596 * crash_nasm equ 0
597 * To avoid such a crash, such requests are silently discarded.
598 * This may not be the best solution.
600 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
601 bsym = raa_write(bsym, segment, nglobs);
602 } else if (sym->section != SHN_ABS) {
604 * This is a global symbol; so we must add it to the rbtree
605 * of global symbols in its section.
607 * In addition, we check the special text for symbol
608 * type and size information.
610 sects[sym->section-1]->gsyms =
611 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
613 if (special) {
614 int n = strcspn(special, " \t");
616 if (!nasm_strnicmp(special, "function", n))
617 sym->type |= STT_FUNC;
618 else if (!nasm_strnicmp(special, "data", n) ||
619 !nasm_strnicmp(special, "object", n))
620 sym->type |= STT_OBJECT;
621 else if (!nasm_strnicmp(special, "notype", n))
622 sym->type |= STT_NOTYPE;
623 else
624 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
625 n, special);
626 special += n;
628 while (nasm_isspace(*special))
629 ++special;
630 if (*special) {
631 n = strcspn(special, " \t");
632 if (!nasm_strnicmp(special, "default", n))
633 sym->other = STV_DEFAULT;
634 else if (!nasm_strnicmp(special, "internal", n))
635 sym->other = STV_INTERNAL;
636 else if (!nasm_strnicmp(special, "hidden", n))
637 sym->other = STV_HIDDEN;
638 else if (!nasm_strnicmp(special, "protected", n))
639 sym->other = STV_PROTECTED;
640 else
641 n = 0;
642 special += n;
645 if (*special) {
646 struct tokenval tokval;
647 expr *e;
648 int fwd = 0;
649 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
651 while (special[n] && nasm_isspace(special[n]))
652 n++;
654 * We have a size expression; attempt to
655 * evaluate it.
657 stdscan_reset();
658 stdscan_bufptr = special + n;
659 tokval.t_type = TOKEN_INVALID;
660 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
661 NULL);
662 if (fwd) {
663 sym->nextfwd = fwds;
664 fwds = sym;
665 sym->name = nasm_strdup(name);
666 } else if (e) {
667 if (!is_simple(e))
668 error(ERR_NONFATAL, "cannot use relocatable"
669 " expression as symbol size");
670 else
671 sym->size = reloc_value(e);
673 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
675 special_used = true;
678 * If TLS segment, mark symbol accordingly.
680 if (sects[sym->section - 1]->flags & SHF_TLS) {
681 sym->type &= 0xf0;
682 sym->type |= STT_TLS;
685 sym->globnum = nglobs;
686 nglobs++;
687 } else
688 nlocals++;
690 if (special && !special_used)
691 error(ERR_NONFATAL, "no special symbol features supported here");
694 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
696 struct Reloc *r;
698 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
699 sect->tail = &r->next;
700 r->next = NULL;
702 r->address = sect->len;
703 if (segment == NO_SEG)
704 r->symbol = 0;
705 else {
706 int i;
707 r->symbol = 0;
708 for (i = 0; i < nsects; i++)
709 if (segment == sects[i]->index)
710 r->symbol = i + 2;
711 if (!r->symbol)
712 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
714 r->type = type;
716 sect->nrelocs++;
720 * This routine deals with ..got and ..sym relocations: the more
721 * complicated kinds. In shared-library writing, some relocations
722 * with respect to global symbols must refer to the precise symbol
723 * rather than referring to an offset from the base of the section
724 * _containing_ the symbol. Such relocations call to this routine,
725 * which searches the symbol list for the symbol in question.
727 * R_386_GOT32 references require the _exact_ symbol address to be
728 * used; R_386_32 references can be at an offset from the symbol.
729 * The boolean argument `exact' tells us this.
731 * Return value is the adjusted value of `addr', having become an
732 * offset from the symbol rather than the section. Should always be
733 * zero when returning from an exact call.
735 * Limitation: if you define two symbols at the same place,
736 * confusion will occur.
738 * Inefficiency: we search, currently, using a linked list which
739 * isn't even necessarily sorted.
741 static int32_t elf_add_gsym_reloc(struct Section *sect,
742 int32_t segment, uint32_t offset,
743 int type, bool exact)
745 struct Reloc *r;
746 struct Section *s;
747 struct Symbol *sym;
748 struct rbtree *srb;
749 int i;
752 * First look up the segment/offset pair and find a global
753 * symbol corresponding to it. If it's not one of our segments,
754 * then it must be an external symbol, in which case we're fine
755 * doing a normal elf_add_reloc after first sanity-checking
756 * that the offset from the symbol is zero.
758 s = NULL;
759 for (i = 0; i < nsects; i++)
760 if (segment == sects[i]->index) {
761 s = sects[i];
762 break;
764 if (!s) {
765 if (exact && offset != 0)
766 error(ERR_NONFATAL, "unable to find a suitable global symbol"
767 " for this reference");
768 else
769 elf_add_reloc(sect, segment, type);
770 return offset;
773 srb = rb_search(s->gsyms, offset);
774 if (!srb || (exact && srb->key != offset)) {
775 error(ERR_NONFATAL, "unable to find a suitable global symbol"
776 " for this reference");
777 return 0;
779 sym = container_of(srb, struct Symbol, symv);
781 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
782 sect->tail = &r->next;
783 r->next = NULL;
785 r->address = sect->len;
786 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
787 r->type = type;
789 sect->nrelocs++;
791 return offset - sym->symv.key;
794 static void elf_out(int32_t segto, const void *data,
795 enum out_type type, uint64_t size,
796 int32_t segment, int32_t wrt)
798 struct Section *s;
799 int32_t addr;
800 uint8_t mydata[4], *p;
801 int i;
802 static struct symlininfo sinfo;
805 * handle absolute-assembly (structure definitions)
807 if (segto == NO_SEG) {
808 if (type != OUT_RESERVE)
809 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
810 " space");
811 return;
814 s = NULL;
815 for (i = 0; i < nsects; i++)
816 if (segto == sects[i]->index) {
817 s = sects[i];
818 break;
820 if (!s) {
821 int tempint; /* ignored */
822 if (segto != elf_section_names(".text", 2, &tempint))
823 error(ERR_PANIC, "strange segment conditions in ELF driver");
824 else {
825 s = sects[nsects - 1];
826 i = nsects - 1;
830 /* again some stabs debugging stuff */
831 if (of_elf32.current_dfmt) {
832 sinfo.offset = s->len;
833 sinfo.section = i;
834 sinfo.name = s->name;
835 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
837 /* end of debugging stuff */
839 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
840 error(ERR_WARNING, "attempt to initialize memory in"
841 " BSS section `%s': ignored", s->name);
842 s->len += realsize(type, size);
843 return;
846 if (type == OUT_RESERVE) {
847 if (s->type == SHT_PROGBITS) {
848 error(ERR_WARNING, "uninitialized space declared in"
849 " non-BSS section `%s': zeroing", s->name);
850 elf_sect_write(s, NULL, size);
851 } else
852 s->len += size;
853 } else if (type == OUT_RAWDATA) {
854 if (segment != NO_SEG)
855 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
856 elf_sect_write(s, data, size);
857 } else if (type == OUT_ADDRESS) {
858 bool gnu16 = false;
859 addr = *(int64_t *)data;
860 if (segment != NO_SEG) {
861 if (segment % 2) {
862 error(ERR_NONFATAL, "ELF format does not support"
863 " segment base references");
864 } else {
865 if (wrt == NO_SEG) {
866 if (size == 2) {
867 gnu16 = true;
868 elf_add_reloc(s, segment, R_386_16);
869 } else {
870 elf_add_reloc(s, segment, R_386_32);
872 } else if (wrt == elf_gotpc_sect + 1) {
874 * The user will supply GOT relative to $$. ELF
875 * will let us have GOT relative to $. So we
876 * need to fix up the data item by $-$$.
878 addr += s->len;
879 elf_add_reloc(s, segment, R_386_GOTPC);
880 } else if (wrt == elf_gotoff_sect + 1) {
881 elf_add_reloc(s, segment, R_386_GOTOFF);
882 } else if (wrt == elf_tlsie_sect + 1) {
883 addr = elf_add_gsym_reloc(s, segment, addr,
884 R_386_TLS_IE, true);
885 } else if (wrt == elf_got_sect + 1) {
886 addr = elf_add_gsym_reloc(s, segment, addr,
887 R_386_GOT32, true);
888 } else if (wrt == elf_sym_sect + 1) {
889 if (size == 2) {
890 gnu16 = true;
891 addr = elf_add_gsym_reloc(s, segment, addr,
892 R_386_16, false);
893 } else {
894 addr = elf_add_gsym_reloc(s, segment, addr,
895 R_386_32, false);
897 } else if (wrt == elf_plt_sect + 1) {
898 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
899 "relative PLT references");
900 } else {
901 error(ERR_NONFATAL, "ELF format does not support this"
902 " use of WRT");
903 wrt = NO_SEG; /* we can at least _try_ to continue */
907 p = mydata;
908 if (gnu16) {
909 error(ERR_WARNING | ERR_WARN_GNUELF,
910 "16-bit relocations in ELF is a GNU extension");
911 WRITESHORT(p, addr);
912 } else {
913 if (size != 4 && segment != NO_SEG) {
914 error(ERR_NONFATAL,
915 "Unsupported non-32-bit ELF relocation");
917 WRITELONG(p, addr);
919 elf_sect_write(s, mydata, size);
920 } else if (type == OUT_REL2ADR) {
921 if (segment == segto)
922 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
923 if (segment != NO_SEG && segment % 2) {
924 error(ERR_NONFATAL, "ELF format does not support"
925 " segment base references");
926 } else {
927 if (wrt == NO_SEG) {
928 error(ERR_WARNING | ERR_WARN_GNUELF,
929 "16-bit relocations in ELF is a GNU extension");
930 elf_add_reloc(s, segment, R_386_PC16);
931 } else {
932 error(ERR_NONFATAL,
933 "Unsupported non-32-bit ELF relocation");
936 p = mydata;
937 WRITESHORT(p, *(int64_t *)data - size);
938 elf_sect_write(s, mydata, 2L);
939 } else if (type == OUT_REL4ADR) {
940 if (segment == segto)
941 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
942 if (segment != NO_SEG && segment % 2) {
943 error(ERR_NONFATAL, "ELF format does not support"
944 " segment base references");
945 } else {
946 if (wrt == NO_SEG) {
947 elf_add_reloc(s, segment, R_386_PC32);
948 } else if (wrt == elf_plt_sect + 1) {
949 elf_add_reloc(s, segment, R_386_PLT32);
950 } else if (wrt == elf_gotpc_sect + 1 ||
951 wrt == elf_gotoff_sect + 1 ||
952 wrt == elf_got_sect + 1) {
953 error(ERR_NONFATAL, "ELF format cannot produce PC-"
954 "relative GOT references");
955 } else {
956 error(ERR_NONFATAL, "ELF format does not support this"
957 " use of WRT");
958 wrt = NO_SEG; /* we can at least _try_ to continue */
961 p = mydata;
962 WRITELONG(p, *(int64_t *)data - size);
963 elf_sect_write(s, mydata, 4L);
967 static void elf_write(void)
969 int align;
970 char *p;
971 int i;
973 struct SAA *symtab;
974 int32_t symtablen, symtablocal;
977 * Work out how many sections we will have. We have SHN_UNDEF,
978 * then the flexible user sections, then the fixed sections
979 * `.shstrtab', `.symtab' and `.strtab', then optionally
980 * relocation sections for the user sections.
982 nsections = sec_numspecial + 1;
983 if (of_elf32.current_dfmt == &df_stabs)
984 nsections += 3;
985 else if (of_elf32.current_dfmt == &df_dwarf)
986 nsections += 10;
988 add_sectname("", ".shstrtab");
989 add_sectname("", ".symtab");
990 add_sectname("", ".strtab");
991 for (i = 0; i < nsects; i++) {
992 nsections++; /* for the section itself */
993 if (sects[i]->head) {
994 nsections++; /* for its relocations */
995 add_sectname(".rel", sects[i]->name);
999 if (of_elf32.current_dfmt == &df_stabs) {
1000 /* in case the debug information is wanted, just add these three sections... */
1001 add_sectname("", ".stab");
1002 add_sectname("", ".stabstr");
1003 add_sectname(".rel", ".stab");
1004 } else if (of_elf32.current_dfmt == &df_dwarf) {
1005 /* the dwarf debug standard specifies the following ten sections,
1006 not all of which are currently implemented,
1007 although all of them are defined. */
1008 add_sectname("", ".debug_aranges");
1009 add_sectname(".rela", ".debug_aranges");
1010 add_sectname("", ".debug_pubnames");
1011 add_sectname("", ".debug_info");
1012 add_sectname(".rela", ".debug_info");
1013 add_sectname("", ".debug_abbrev");
1014 add_sectname("", ".debug_line");
1015 add_sectname(".rela", ".debug_line");
1016 add_sectname("", ".debug_frame");
1017 add_sectname("", ".debug_loc");
1021 * Output the ELF header.
1023 fwrite("\177ELF\1\1\1", 7, 1, elffp);
1024 fputc(elf_osabi, elffp);
1025 fputc(elf_abiver, elffp);
1026 fwritezero(7, elffp);
1027 fwriteint16_t(1, elffp); /* ET_REL relocatable file */
1028 fwriteint16_t(3, elffp); /* EM_386 processor ID */
1029 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1030 fwriteint32_t(0L, elffp); /* no entry point */
1031 fwriteint32_t(0L, elffp); /* no program header table */
1032 fwriteint32_t(0x40L, elffp); /* section headers straight after
1033 * ELF header plus alignment */
1034 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1035 fwriteint16_t(0x34, elffp); /* size of ELF header */
1036 fwriteint16_t(0, elffp); /* no program header table, again */
1037 fwriteint16_t(0, elffp); /* still no program header table */
1038 fwriteint16_t(0x28, elffp); /* size of section header */
1039 fwriteint16_t(nsections, elffp); /* number of sections */
1040 fwriteint16_t(sec_shstrtab, elffp); /* string table section index for
1041 * section header table */
1042 fwriteint32_t(0L, elffp); /* align to 0x40 bytes */
1043 fwriteint32_t(0L, elffp);
1044 fwriteint32_t(0L, elffp);
1047 * Build the symbol table and relocation tables.
1049 symtab = elf_build_symtab(&symtablen, &symtablocal);
1050 for (i = 0; i < nsects; i++)
1051 if (sects[i]->head)
1052 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1053 sects[i]->head);
1056 * Now output the section header table.
1059 elf_foffs = 0x40 + 0x28 * nsections;
1060 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1061 elf_foffs += align;
1062 elf_nsect = 0;
1063 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1065 /* SHN_UNDEF */
1066 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1067 p = shstrtab + 1;
1069 /* The normal sections */
1070 for (i = 0; i < nsects; i++) {
1071 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1072 (sects[i]->type == SHT_PROGBITS ?
1073 sects[i]->data : NULL), true,
1074 sects[i]->len, 0, 0, sects[i]->align, 0);
1075 p += strlen(p) + 1;
1078 /* .shstrtab */
1079 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1080 shstrtablen, 0, 0, 1, 0);
1081 p += strlen(p) + 1;
1083 /* .symtab */
1084 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1085 symtablen, sec_strtab, symtablocal, 4, 16);
1086 p += strlen(p) + 1;
1088 /* .strtab */
1089 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1090 strslen, 0, 0, 1, 0);
1091 p += strlen(p) + 1;
1093 /* The relocation sections */
1094 for (i = 0; i < nsects; i++)
1095 if (sects[i]->head) {
1096 elf_section_header(p - shstrtab, SHT_REL, 0, sects[i]->rel, true,
1097 sects[i]->rellen, sec_symtab, i + 1, 4, 8);
1098 p += strlen(p) + 1;
1102 if (of_elf32.current_dfmt == &df_stabs) {
1103 /* for debugging information, create the last three sections
1104 which are the .stab , .stabstr and .rel.stab sections respectively */
1106 /* this function call creates the stab sections in memory */
1107 stabs32_generate();
1109 if (stabbuf && stabstrbuf && stabrelbuf) {
1110 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1111 stablen, sec_stabstr, 0, 4, 12);
1112 p += strlen(p) + 1;
1114 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1115 stabstrlen, 0, 0, 4, 0);
1116 p += strlen(p) + 1;
1118 /* link -> symtable info -> section to refer to */
1119 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1120 stabrellen, sec_symtab, sec_stab, 4, 8);
1121 p += strlen(p) + 1;
1123 } else if (of_elf32.current_dfmt == &df_dwarf) {
1124 /* for dwarf debugging information, create the ten dwarf sections */
1126 /* this function call creates the dwarf sections in memory */
1127 if (dwarf_fsect)
1128 dwarf32_generate();
1130 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1131 arangeslen, 0, 0, 1, 0);
1132 p += strlen(p) + 1;
1134 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1135 arangesrellen, sec_symtab, sec_debug_aranges,
1136 1, 12);
1137 p += strlen(p) + 1;
1139 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf,
1140 false, pubnameslen, 0, 0, 1, 0);
1141 p += strlen(p) + 1;
1143 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1144 infolen, 0, 0, 1, 0);
1145 p += strlen(p) + 1;
1147 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1148 inforellen, sec_symtab, sec_debug_info, 1, 12);
1149 p += strlen(p) + 1;
1151 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1152 abbrevlen, 0, 0, 1, 0);
1153 p += strlen(p) + 1;
1155 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1156 linelen, 0, 0, 1, 0);
1157 p += strlen(p) + 1;
1159 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1160 linerellen, sec_symtab, sec_debug_line, 1, 12);
1161 p += strlen(p) + 1;
1163 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1164 framelen, 0, 0, 8, 0);
1165 p += strlen(p) + 1;
1167 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1168 loclen, 0, 0, 1, 0);
1169 p += strlen(p) + 1;
1171 fwritezero(align, elffp);
1174 * Now output the sections.
1176 elf_write_sections();
1178 nasm_free(elf_sects);
1179 saa_free(symtab);
1182 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1184 struct SAA *s = saa_init(1L);
1185 struct Symbol *sym;
1186 uint8_t entry[16], *p;
1187 int i;
1189 *len = *local = 0;
1192 * First, an all-zeros entry, required by the ELF spec.
1194 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1195 *len += 16;
1196 (*local)++;
1199 * Next, an entry for the file name.
1201 p = entry;
1202 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1203 WRITELONG(p, 0); /* no value */
1204 WRITELONG(p, 0); /* no size either */
1205 WRITESHORT(p, STT_FILE); /* type FILE */
1206 WRITESHORT(p, SHN_ABS);
1207 saa_wbytes(s, entry, 16L);
1208 *len += 16;
1209 (*local)++;
1212 * Now some standard symbols defining the segments, for relocation
1213 * purposes.
1215 for (i = 1; i <= nsects; i++) {
1216 p = entry;
1217 WRITELONG(p, 0); /* no symbol name */
1218 WRITELONG(p, 0); /* offset zero */
1219 WRITELONG(p, 0); /* size zero */
1220 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1221 WRITESHORT(p, i); /* section id */
1222 saa_wbytes(s, entry, 16L);
1223 *len += 16;
1224 (*local)++;
1228 * Now the other local symbols.
1230 saa_rewind(syms);
1231 while ((sym = saa_rstruct(syms))) {
1232 if (sym->type & SYM_GLOBAL)
1233 continue;
1234 p = entry;
1235 WRITELONG(p, sym->strpos);
1236 WRITELONG(p, sym->symv.key);
1237 WRITELONG(p, sym->size);
1238 WRITECHAR(p, sym->type); /* type and binding */
1239 WRITECHAR(p, sym->other); /* visibility */
1240 WRITESHORT(p, sym->section);
1241 saa_wbytes(s, entry, 16L);
1242 *len += 16;
1243 (*local)++;
1246 * dwarf needs symbols for debug sections
1247 * which are relocation targets.
1249 //*** fix for 32 bit
1250 if (of_elf32.current_dfmt == &df_dwarf) {
1251 dwarf_infosym = *local;
1252 p = entry;
1253 WRITELONG(p, 0); /* no symbol name */
1254 WRITELONG(p, (uint32_t) 0); /* offset zero */
1255 WRITELONG(p, (uint32_t) 0); /* size zero */
1256 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1257 WRITESHORT(p, sec_debug_info); /* section id */
1258 saa_wbytes(s, entry, 16L);
1259 *len += 16;
1260 (*local)++;
1261 dwarf_abbrevsym = *local;
1262 p = entry;
1263 WRITELONG(p, 0); /* no symbol name */
1264 WRITELONG(p, (uint32_t) 0); /* offset zero */
1265 WRITELONG(p, (uint32_t) 0); /* size zero */
1266 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1267 WRITESHORT(p, sec_debug_abbrev); /* section id */
1268 saa_wbytes(s, entry, 16L);
1269 *len += 16;
1270 (*local)++;
1271 dwarf_linesym = *local;
1272 p = entry;
1273 WRITELONG(p, 0); /* no symbol name */
1274 WRITELONG(p, (uint32_t) 0); /* offset zero */
1275 WRITELONG(p, (uint32_t) 0); /* size zero */
1276 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1277 WRITESHORT(p, sec_debug_line); /* section id */
1278 saa_wbytes(s, entry, 16L);
1279 *len += 16;
1280 (*local)++;
1284 * Now the global symbols.
1286 saa_rewind(syms);
1287 while ((sym = saa_rstruct(syms))) {
1288 if (!(sym->type & SYM_GLOBAL))
1289 continue;
1290 p = entry;
1291 WRITELONG(p, sym->strpos);
1292 WRITELONG(p, sym->symv.key);
1293 WRITELONG(p, sym->size);
1294 WRITECHAR(p, sym->type); /* type and binding */
1295 WRITECHAR(p, sym->other); /* visibility */
1296 WRITESHORT(p, sym->section);
1297 saa_wbytes(s, entry, 16L);
1298 *len += 16;
1301 return s;
1304 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1306 struct SAA *s;
1307 uint8_t *p, entry[8];
1308 int32_t global_offset;
1310 if (!r)
1311 return NULL;
1313 s = saa_init(1L);
1314 *len = 0;
1317 * How to onvert from a global placeholder to a real symbol index;
1318 * the +2 refers to the two special entries, the null entry and
1319 * the filename entry.
1321 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1323 while (r) {
1324 int32_t sym = r->symbol;
1327 * Create a real symbol index; the +2 refers to the two special
1328 * entries, the null entry and the filename entry.
1330 if (sym >= GLOBAL_TEMP_BASE)
1331 sym += global_offset;
1333 p = entry;
1334 WRITELONG(p, r->address);
1335 WRITELONG(p, (sym << 8) + r->type);
1336 saa_wbytes(s, entry, 8L);
1337 *len += 8;
1339 r = r->next;
1342 return s;
1345 static void elf_section_header(int name, int type, int flags,
1346 void *data, bool is_saa, int32_t datalen,
1347 int link, int info, int align, int eltsize)
1349 elf_sects[elf_nsect].data = data;
1350 elf_sects[elf_nsect].len = datalen;
1351 elf_sects[elf_nsect].is_saa = is_saa;
1352 elf_nsect++;
1354 fwriteint32_t((int32_t)name, elffp);
1355 fwriteint32_t((int32_t)type, elffp);
1356 fwriteint32_t((int32_t)flags, elffp);
1357 fwriteint32_t(0L, elffp); /* no address, ever, in object files */
1358 fwriteint32_t(type == 0 ? 0L : elf_foffs, elffp);
1359 fwriteint32_t(datalen, elffp);
1360 if (data)
1361 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1362 fwriteint32_t((int32_t)link, elffp);
1363 fwriteint32_t((int32_t)info, elffp);
1364 fwriteint32_t((int32_t)align, elffp);
1365 fwriteint32_t((int32_t)eltsize, elffp);
1368 static void elf_write_sections(void)
1370 int i;
1371 for (i = 0; i < elf_nsect; i++)
1372 if (elf_sects[i].data) {
1373 int32_t len = elf_sects[i].len;
1374 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1375 int32_t align = reallen - len;
1376 if (elf_sects[i].is_saa)
1377 saa_fpwrite(elf_sects[i].data, elffp);
1378 else
1379 fwrite(elf_sects[i].data, len, 1, elffp);
1380 fwritezero(align, elffp);
1384 static void elf_sect_write(struct Section *sect,
1385 const uint8_t *data, uint32_t len)
1387 saa_wbytes(sect->data, data, len);
1388 sect->len += len;
1391 static int32_t elf_segbase(int32_t segment)
1393 return segment;
1396 static int elf_directive(char *directive, char *value, int pass)
1398 bool err;
1399 int64_t n;
1400 char *p;
1402 if (!strcmp(directive, "osabi")) {
1403 if (pass == 2)
1404 return 1; /* ignore in pass 2 */
1406 n = readnum(value, &err);
1407 if (err) {
1408 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1409 return 1;
1411 if (n < 0 || n > 255) {
1412 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1413 return 1;
1415 elf_osabi = n;
1416 elf_abiver = 0;
1418 if ((p = strchr(value,',')) == NULL)
1419 return 1;
1421 n = readnum(p+1, &err);
1422 if (err || n < 0 || n > 255) {
1423 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1424 return 1;
1427 elf_abiver = n;
1428 return 1;
1431 return 0;
1434 static void elf_filename(char *inname, char *outname, efunc error)
1436 strcpy(elf_module, inname);
1437 standard_extension(inname, outname, ".o", error);
1440 extern macros_t elf_stdmac[];
1442 static int elf_set_info(enum geninfo type, char **val)
1444 (void)type;
1445 (void)val;
1446 return 0;
1448 static struct dfmt df_dwarf = {
1449 "ELF32 (i386) dwarf debug format for Linux/Unix",
1450 "dwarf",
1451 dwarf32_init,
1452 dwarf32_linenum,
1453 debug32_deflabel,
1454 debug32_directive,
1455 debug32_typevalue,
1456 dwarf32_output,
1457 dwarf32_cleanup
1459 static struct dfmt df_stabs = {
1460 "ELF32 (i386) stabs debug format for Linux/Unix",
1461 "stabs",
1462 null_debug_init,
1463 stabs32_linenum,
1464 debug32_deflabel,
1465 debug32_directive,
1466 debug32_typevalue,
1467 stabs32_output,
1468 stabs32_cleanup
1471 struct dfmt *elf32_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1473 struct ofmt of_elf32 = {
1474 "ELF32 (i386) object files (e.g. Linux)",
1475 "elf32",
1477 elf32_debugs_arr,
1478 &df_stabs,
1479 elf_stdmac,
1480 elf_init,
1481 elf_set_info,
1482 elf_out,
1483 elf_deflabel,
1484 elf_section_names,
1485 elf_segbase,
1486 elf_directive,
1487 elf_filename,
1488 elf_cleanup
1491 struct ofmt of_elf = {
1492 "ELF (short name for ELF32) ",
1493 "elf",
1495 elf32_debugs_arr,
1496 &df_stabs,
1497 elf_stdmac,
1498 elf_init_hack,
1499 elf_set_info,
1500 elf_out,
1501 elf_deflabel,
1502 elf_section_names,
1503 elf_segbase,
1504 elf_directive,
1505 elf_filename,
1506 elf_cleanup
1508 /* again, the stabs debugging stuff (code) */
1510 static void stabs32_linenum(const char *filename, int32_t linenumber,
1511 int32_t segto)
1513 (void)segto;
1515 if (!stabs_filename) {
1516 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1517 strcpy(stabs_filename, filename);
1518 } else {
1519 if (strcmp(stabs_filename, filename)) {
1520 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1521 in fact, this leak comes in quite handy to maintain a list of files
1522 encountered so far in the symbol lines... */
1524 /* why not nasm_free(stabs_filename); we're done with the old one */
1526 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1527 strcpy(stabs_filename, filename);
1530 debug_immcall = 1;
1531 currentline = linenumber;
1534 static void debug32_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1535 char *special)
1537 (void)name;
1538 (void)segment;
1539 (void)offset;
1540 (void)is_global;
1541 (void)special;
1544 static void debug32_directive(const char *directive, const char *params)
1546 (void)directive;
1547 (void)params;
1550 static void debug32_typevalue(int32_t type)
1552 int32_t stype, ssize;
1553 switch (TYM_TYPE(type)) {
1554 case TY_LABEL:
1555 ssize = 0;
1556 stype = STT_NOTYPE;
1557 break;
1558 case TY_BYTE:
1559 ssize = 1;
1560 stype = STT_OBJECT;
1561 break;
1562 case TY_WORD:
1563 ssize = 2;
1564 stype = STT_OBJECT;
1565 break;
1566 case TY_DWORD:
1567 ssize = 4;
1568 stype = STT_OBJECT;
1569 break;
1570 case TY_FLOAT:
1571 ssize = 4;
1572 stype = STT_OBJECT;
1573 break;
1574 case TY_QWORD:
1575 ssize = 8;
1576 stype = STT_OBJECT;
1577 break;
1578 case TY_TBYTE:
1579 ssize = 10;
1580 stype = STT_OBJECT;
1581 break;
1582 case TY_OWORD:
1583 ssize = 8;
1584 stype = STT_OBJECT;
1585 break;
1586 case TY_COMMON:
1587 ssize = 0;
1588 stype = STT_COMMON;
1589 break;
1590 case TY_SEG:
1591 ssize = 0;
1592 stype = STT_SECTION;
1593 break;
1594 case TY_EXTERN:
1595 ssize = 0;
1596 stype = STT_NOTYPE;
1597 break;
1598 case TY_EQU:
1599 ssize = 0;
1600 stype = STT_NOTYPE;
1601 break;
1602 default:
1603 ssize = 0;
1604 stype = STT_NOTYPE;
1605 break;
1607 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1608 lastsym->size = ssize;
1609 lastsym->type = stype;
1613 static void stabs32_output(int type, void *param)
1615 struct symlininfo *s;
1616 struct linelist *el;
1617 if (type == TY_STABSSYMLIN) {
1618 if (debug_immcall) {
1619 s = (struct symlininfo *)param;
1620 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1621 return; /* we are only interested in the text stuff */
1622 numlinestabs++;
1623 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1624 el->info.offset = s->offset;
1625 el->info.section = s->section;
1626 el->info.name = s->name;
1627 el->line = currentline;
1628 el->filename = stabs_filename;
1629 el->next = 0;
1630 if (stabslines) {
1631 stabslines->last->next = el;
1632 stabslines->last = el;
1633 } else {
1634 stabslines = el;
1635 stabslines->last = el;
1639 debug_immcall = 0;
1642 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1643 do {\
1644 WRITELONG(p,n_strx); \
1645 WRITECHAR(p,n_type); \
1646 WRITECHAR(p,n_other); \
1647 WRITESHORT(p,n_desc); \
1648 WRITELONG(p,n_value); \
1649 } while (0)
1651 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1653 static void stabs32_generate(void)
1655 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1656 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1657 char **allfiles;
1658 int *fileidx;
1660 struct linelist *ptr;
1662 ptr = stabslines;
1664 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1665 for (i = 0; i < numlinestabs; i++)
1666 allfiles[i] = 0;
1667 numfiles = 0;
1668 while (ptr) {
1669 if (numfiles == 0) {
1670 allfiles[0] = ptr->filename;
1671 numfiles++;
1672 } else {
1673 for (i = 0; i < numfiles; i++) {
1674 if (!strcmp(allfiles[i], ptr->filename))
1675 break;
1677 if (i >= numfiles) {
1678 allfiles[i] = ptr->filename;
1679 numfiles++;
1682 ptr = ptr->next;
1684 strsize = 1;
1685 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1686 for (i = 0; i < numfiles; i++) {
1687 fileidx[i] = strsize;
1688 strsize += strlen(allfiles[i]) + 1;
1690 mainfileindex = 0;
1691 for (i = 0; i < numfiles; i++) {
1692 if (!strcmp(allfiles[i], elf_module)) {
1693 mainfileindex = i;
1694 break;
1698 /* worst case size of the stab buffer would be:
1699 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1701 sbuf =
1702 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1703 sizeof(struct stabentry));
1705 ssbuf = (uint8_t *)nasm_malloc(strsize);
1707 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1708 rptr = rbuf;
1710 for (i = 0; i < numfiles; i++) {
1711 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1713 ssbuf[0] = 0;
1715 stabstrlen = strsize; /* set global variable for length of stab strings */
1717 sptr = sbuf;
1718 ptr = stabslines;
1719 numstabs = 0;
1721 if (ptr) {
1722 /* this is the first stab, its strx points to the filename of the
1723 the source-file, the n_desc field should be set to the number
1724 of remaining stabs
1726 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1728 /* this is the stab for the main source file */
1729 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1731 /* relocation table entry */
1733 /* Since the symbol table has two entries before */
1734 /* the section symbols, the index in the info.section */
1735 /* member must be adjusted by adding 2 */
1737 WRITELONG(rptr, (sptr - sbuf) - 4);
1738 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1740 numstabs++;
1741 currfile = mainfileindex;
1744 while (ptr) {
1745 if (strcmp(allfiles[currfile], ptr->filename)) {
1746 /* oops file has changed... */
1747 for (i = 0; i < numfiles; i++)
1748 if (!strcmp(allfiles[i], ptr->filename))
1749 break;
1750 currfile = i;
1751 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1752 ptr->info.offset);
1753 numstabs++;
1755 /* relocation table entry */
1756 WRITELONG(rptr, (sptr - sbuf) - 4);
1757 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1760 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1761 numstabs++;
1763 /* relocation table entry */
1765 WRITELONG(rptr, (sptr - sbuf) - 4);
1766 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1768 ptr = ptr->next;
1772 ((struct stabentry *)sbuf)->n_desc = numstabs;
1774 nasm_free(allfiles);
1775 nasm_free(fileidx);
1777 stablen = (sptr - sbuf);
1778 stabrellen = (rptr - rbuf);
1779 stabrelbuf = rbuf;
1780 stabbuf = sbuf;
1781 stabstrbuf = ssbuf;
1784 static void stabs32_cleanup(void)
1786 struct linelist *ptr, *del;
1787 if (!stabslines)
1788 return;
1789 ptr = stabslines;
1790 while (ptr) {
1791 del = ptr;
1792 ptr = ptr->next;
1793 nasm_free(del);
1795 if (stabbuf)
1796 nasm_free(stabbuf);
1797 if (stabrelbuf)
1798 nasm_free(stabrelbuf);
1799 if (stabstrbuf)
1800 nasm_free(stabstrbuf);
1803 /* dwarf routines */
1805 static void dwarf32_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1807 (void)of;
1808 (void)id;
1809 (void)fp;
1810 (void)error;
1812 ndebugs = 3; /* 3 debug symbols */
1815 static void dwarf32_linenum(const char *filename, int32_t linenumber,
1816 int32_t segto)
1818 (void)segto;
1819 dwarf32_findfile(filename);
1820 debug_immcall = 1;
1821 currentline = linenumber;
1824 /* called from elf_out with type == TY_DEBUGSYMLIN */
1825 static void dwarf32_output(int type, void *param)
1827 int ln, aa, inx, maxln, soc;
1828 struct symlininfo *s;
1829 struct SAA *plinep;
1831 (void)type;
1833 s = (struct symlininfo *)param;
1834 /* line number info is only gathered for executable sections */
1835 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1836 return;
1837 /* Check if section index has changed */
1838 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1840 dwarf32_findsect(s->section);
1842 /* do nothing unless line or file has changed */
1843 if (debug_immcall)
1845 ln = currentline - dwarf_csect->line;
1846 aa = s->offset - dwarf_csect->offset;
1847 inx = dwarf_clist->line;
1848 plinep = dwarf_csect->psaa;
1849 /* check for file change */
1850 if (!(inx == dwarf_csect->file))
1852 saa_write8(plinep,DW_LNS_set_file);
1853 saa_write8(plinep,inx);
1854 dwarf_csect->file = inx;
1856 /* check for line change */
1857 if (ln)
1859 /* test if in range of special op code */
1860 maxln = line_base + line_range;
1861 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1862 if (ln >= line_base && ln < maxln && soc < 256)
1864 saa_write8(plinep,soc);
1866 else
1868 if (ln)
1870 saa_write8(plinep,DW_LNS_advance_line);
1871 saa_wleb128s(plinep,ln);
1873 if (aa)
1875 saa_write8(plinep,DW_LNS_advance_pc);
1876 saa_wleb128u(plinep,aa);
1879 dwarf_csect->line = currentline;
1880 dwarf_csect->offset = s->offset;
1882 /* show change handled */
1883 debug_immcall = 0;
1888 static void dwarf32_generate(void)
1890 uint8_t *pbuf;
1891 int indx;
1892 struct linelist *ftentry;
1893 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1894 struct SAA *parangesrel, *plinesrel, *pinforel;
1895 struct sectlist *psect;
1896 size_t saalen, linepoff, totlen, highaddr;
1898 /* write epilogues for each line program range */
1899 /* and build aranges section */
1900 paranges = saa_init(1L);
1901 parangesrel = saa_init(1L);
1902 saa_write16(paranges,2); /* dwarf version */
1903 saa_write32(parangesrel, paranges->datalen+4);
1904 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
1905 saa_write32(parangesrel, 0);
1906 saa_write32(paranges,0); /* offset into info */
1907 saa_write8(paranges,4); /* pointer size */
1908 saa_write8(paranges,0); /* not segmented */
1909 saa_write32(paranges,0); /* padding */
1910 /* iterate though sectlist entries */
1911 psect = dwarf_fsect;
1912 totlen = 0;
1913 highaddr = 0;
1914 for (indx = 0; indx < dwarf_nsections; indx++)
1916 plinep = psect->psaa;
1917 /* Line Number Program Epilogue */
1918 saa_write8(plinep,2); /* std op 2 */
1919 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1920 saa_write8(plinep,DW_LNS_extended_op);
1921 saa_write8(plinep,1); /* operand length */
1922 saa_write8(plinep,DW_LNE_end_sequence);
1923 totlen += plinep->datalen;
1924 /* range table relocation entry */
1925 saa_write32(parangesrel, paranges->datalen + 4);
1926 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
1927 saa_write32(parangesrel, (uint32_t) 0);
1928 /* range table entry */
1929 saa_write32(paranges,0x0000); /* range start */
1930 saa_write32(paranges,sects[psect->section]->len); /* range length */
1931 highaddr += sects[psect->section]->len;
1932 /* done with this entry */
1933 psect = psect->next;
1935 saa_write32(paranges,0); /* null address */
1936 saa_write32(paranges,0); /* null length */
1937 saalen = paranges->datalen;
1938 arangeslen = saalen + 4;
1939 arangesbuf = pbuf = nasm_malloc(arangeslen);
1940 WRITELONG(pbuf,saalen); /* initial length */
1941 saa_rnbytes(paranges, pbuf, saalen);
1942 saa_free(paranges);
1944 /* build rela.aranges section */
1945 arangesrellen = saalen = parangesrel->datalen;
1946 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1947 saa_rnbytes(parangesrel, pbuf, saalen);
1948 saa_free(parangesrel);
1950 /* build pubnames section */
1951 ppubnames = saa_init(1L);
1952 saa_write16(ppubnames,3); /* dwarf version */
1953 saa_write32(ppubnames,0); /* offset into info */
1954 saa_write32(ppubnames,0); /* space used in info */
1955 saa_write32(ppubnames,0); /* end of list */
1956 saalen = ppubnames->datalen;
1957 pubnameslen = saalen + 4;
1958 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1959 WRITELONG(pbuf,saalen); /* initial length */
1960 saa_rnbytes(ppubnames, pbuf, saalen);
1961 saa_free(ppubnames);
1963 /* build info section */
1964 pinfo = saa_init(1L);
1965 pinforel = saa_init(1L);
1966 saa_write16(pinfo,2); /* dwarf version */
1967 saa_write32(pinforel, pinfo->datalen + 4);
1968 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
1969 saa_write32(pinforel, 0);
1970 saa_write32(pinfo,0); /* offset into abbrev */
1971 saa_write8(pinfo,4); /* pointer size */
1972 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1973 saa_write32(pinforel, pinfo->datalen + 4);
1974 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1975 saa_write32(pinforel, 0);
1976 saa_write32(pinfo,0); /* DW_AT_low_pc */
1977 saa_write32(pinforel, pinfo->datalen + 4);
1978 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1979 saa_write32(pinforel, 0);
1980 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
1981 saa_write32(pinforel, pinfo->datalen + 4);
1982 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
1983 saa_write32(pinforel, 0);
1984 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1985 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1986 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1987 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1988 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1989 saa_write32(pinforel, pinfo->datalen + 4);
1990 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1991 saa_write32(pinforel, 0);
1992 saa_write32(pinfo,0); /* DW_AT_low_pc */
1993 saa_write32(pinfo,0); /* DW_AT_frame_base */
1994 saa_write8(pinfo,0); /* end of entries */
1995 saalen = pinfo->datalen;
1996 infolen = saalen + 4;
1997 infobuf = pbuf = nasm_malloc(infolen);
1998 WRITELONG(pbuf,saalen); /* initial length */
1999 saa_rnbytes(pinfo, pbuf, saalen);
2000 saa_free(pinfo);
2002 /* build rela.info section */
2003 inforellen = saalen = pinforel->datalen;
2004 inforelbuf = pbuf = nasm_malloc(inforellen);
2005 saa_rnbytes(pinforel, pbuf, saalen);
2006 saa_free(pinforel);
2008 /* build abbrev section */
2009 pabbrev = saa_init(1L);
2010 saa_write8(pabbrev,1); /* entry number LEB128u */
2011 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2012 saa_write8(pabbrev,1); /* has children */
2013 /* the following attributes and forms are all LEB128u values */
2014 saa_write8(pabbrev,DW_AT_low_pc);
2015 saa_write8(pabbrev,DW_FORM_addr);
2016 saa_write8(pabbrev,DW_AT_high_pc);
2017 saa_write8(pabbrev,DW_FORM_addr);
2018 saa_write8(pabbrev,DW_AT_stmt_list);
2019 saa_write8(pabbrev,DW_FORM_data4);
2020 saa_write8(pabbrev,DW_AT_name);
2021 saa_write8(pabbrev,DW_FORM_string);
2022 saa_write8(pabbrev,DW_AT_producer);
2023 saa_write8(pabbrev,DW_FORM_string);
2024 saa_write8(pabbrev,DW_AT_language);
2025 saa_write8(pabbrev,DW_FORM_data2);
2026 saa_write16(pabbrev,0); /* end of entry */
2027 /* LEB128u usage same as above */
2028 saa_write8(pabbrev,2); /* entry number */
2029 saa_write8(pabbrev,DW_TAG_subprogram);
2030 saa_write8(pabbrev,0); /* no children */
2031 saa_write8(pabbrev,DW_AT_low_pc);
2032 saa_write8(pabbrev,DW_FORM_addr);
2033 saa_write8(pabbrev,DW_AT_frame_base);
2034 saa_write8(pabbrev,DW_FORM_data4);
2035 saa_write16(pabbrev,0); /* end of entry */
2036 abbrevlen = saalen = pabbrev->datalen;
2037 abbrevbuf = pbuf = nasm_malloc(saalen);
2038 saa_rnbytes(pabbrev, pbuf, saalen);
2039 saa_free(pabbrev);
2041 /* build line section */
2042 /* prolog */
2043 plines = saa_init(1L);
2044 saa_write8(plines,1); /* Minimum Instruction Length */
2045 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2046 saa_write8(plines,line_base); /* Line Base */
2047 saa_write8(plines,line_range); /* Line Range */
2048 saa_write8(plines,opcode_base); /* Opcode Base */
2049 /* standard opcode lengths (# of LEB128u operands) */
2050 saa_write8(plines,0); /* Std opcode 1 length */
2051 saa_write8(plines,1); /* Std opcode 2 length */
2052 saa_write8(plines,1); /* Std opcode 3 length */
2053 saa_write8(plines,1); /* Std opcode 4 length */
2054 saa_write8(plines,1); /* Std opcode 5 length */
2055 saa_write8(plines,0); /* Std opcode 6 length */
2056 saa_write8(plines,0); /* Std opcode 7 length */
2057 saa_write8(plines,0); /* Std opcode 8 length */
2058 saa_write8(plines,1); /* Std opcode 9 length */
2059 saa_write8(plines,0); /* Std opcode 10 length */
2060 saa_write8(plines,0); /* Std opcode 11 length */
2061 saa_write8(plines,1); /* Std opcode 12 length */
2062 /* Directory Table */
2063 saa_write8(plines,0); /* End of table */
2064 /* File Name Table */
2065 ftentry = dwarf_flist;
2066 for (indx = 0;indx<dwarf_numfiles;indx++)
2068 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2069 saa_write8(plines,0); /* directory LEB128u */
2070 saa_write8(plines,0); /* time LEB128u */
2071 saa_write8(plines,0); /* size LEB128u */
2072 ftentry = ftentry->next;
2074 saa_write8(plines,0); /* End of table */
2075 linepoff = plines->datalen;
2076 linelen = linepoff + totlen + 10;
2077 linebuf = pbuf = nasm_malloc(linelen);
2078 WRITELONG(pbuf,linelen-4); /* initial length */
2079 WRITESHORT(pbuf,3); /* dwarf version */
2080 WRITELONG(pbuf,linepoff); /* offset to line number program */
2081 /* write line header */
2082 saalen = linepoff;
2083 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2084 pbuf += linepoff;
2085 saa_free(plines);
2086 /* concatonate line program ranges */
2087 linepoff += 13;
2088 plinesrel = saa_init(1L);
2089 psect = dwarf_fsect;
2090 for (indx = 0; indx < dwarf_nsections; indx++)
2092 saa_write32(plinesrel, linepoff);
2093 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
2094 saa_write32(plinesrel, (uint32_t) 0);
2095 plinep = psect->psaa;
2096 saalen = plinep->datalen;
2097 saa_rnbytes(plinep, pbuf, saalen);
2098 pbuf += saalen;
2099 linepoff += saalen;
2100 saa_free(plinep);
2101 /* done with this entry */
2102 psect = psect->next;
2106 /* build rela.lines section */
2107 linerellen =saalen = plinesrel->datalen;
2108 linerelbuf = pbuf = nasm_malloc(linerellen);
2109 saa_rnbytes(plinesrel, pbuf, saalen);
2110 saa_free(plinesrel);
2112 /* build frame section */
2113 framelen = 4;
2114 framebuf = pbuf = nasm_malloc(framelen);
2115 WRITELONG(pbuf,framelen-4); /* initial length */
2117 /* build loc section */
2118 loclen = 16;
2119 locbuf = pbuf = nasm_malloc(loclen);
2120 WRITELONG(pbuf,0); /* null beginning offset */
2121 WRITELONG(pbuf,0); /* null ending offset */
2124 static void dwarf32_cleanup(void)
2126 if (arangesbuf)
2127 nasm_free(arangesbuf);
2128 if (arangesrelbuf)
2129 nasm_free(arangesrelbuf);
2130 if (pubnamesbuf)
2131 nasm_free(pubnamesbuf);
2132 if (infobuf)
2133 nasm_free(infobuf);
2134 if (inforelbuf)
2135 nasm_free(inforelbuf);
2136 if (abbrevbuf)
2137 nasm_free(abbrevbuf);
2138 if (linebuf)
2139 nasm_free(linebuf);
2140 if (linerelbuf)
2141 nasm_free(linerelbuf);
2142 if (framebuf)
2143 nasm_free(framebuf);
2144 if (locbuf)
2145 nasm_free(locbuf);
2147 static void dwarf32_findfile(const char * fname)
2149 int finx;
2150 struct linelist *match;
2152 /* return if fname is current file name */
2153 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2154 /* search for match */
2155 else
2157 match = 0;
2158 if (dwarf_flist)
2160 match = dwarf_flist;
2161 for (finx = 0; finx < dwarf_numfiles; finx++)
2163 if (!(strcmp(fname, match->filename)))
2165 dwarf_clist = match;
2166 return;
2170 /* add file name to end of list */
2171 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2172 dwarf_numfiles++;
2173 dwarf_clist->line = dwarf_numfiles;
2174 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2175 strcpy(dwarf_clist->filename,fname);
2176 dwarf_clist->next = 0;
2177 /* if first entry */
2178 if (!dwarf_flist)
2180 dwarf_flist = dwarf_elist = dwarf_clist;
2181 dwarf_clist->last = 0;
2183 /* chain to previous entry */
2184 else
2186 dwarf_elist->next = dwarf_clist;
2187 dwarf_elist = dwarf_clist;
2191 /* */
2192 static void dwarf32_findsect(const int index)
2194 int sinx;
2195 struct sectlist *match;
2196 struct SAA *plinep;
2197 /* return if index is current section index */
2198 if (dwarf_csect && (dwarf_csect->section == index))
2200 return;
2202 /* search for match */
2203 else
2205 match = 0;
2206 if (dwarf_fsect)
2208 match = dwarf_fsect;
2209 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2211 if ((match->section == index))
2213 dwarf_csect = match;
2214 return;
2216 match = match->next;
2219 /* add entry to end of list */
2220 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2221 dwarf_nsections++;
2222 dwarf_csect->psaa = plinep = saa_init(1L);
2223 dwarf_csect->line = 1;
2224 dwarf_csect->offset = 0;
2225 dwarf_csect->file = 1;
2226 dwarf_csect->section = index;
2227 dwarf_csect->next = 0;
2228 /* set relocatable address at start of line program */
2229 saa_write8(plinep,DW_LNS_extended_op);
2230 saa_write8(plinep,5); /* operand length */
2231 saa_write8(plinep,DW_LNE_set_address);
2232 saa_write32(plinep,0); /* Start Address */
2233 /* if first entry */
2234 if (!dwarf_fsect)
2236 dwarf_fsect = dwarf_esect = dwarf_csect;
2237 dwarf_csect->last = 0;
2239 /* chain to previous entry */
2240 else
2242 dwarf_esect->next = dwarf_csect;
2243 dwarf_esect = dwarf_csect;
2248 #endif /* OF_ELF */