Add copyright notice to insns.dat
[nasm.git] / output / outelf32.c
blob1a327cf837a8bd6dce9e381a50b08f2b0c1a43c9
1 /* outelf.c output routines for the Netwide Assembler to produce
2 * ELF32 (i386 of course) object file format
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the license given in the file "LICENSE"
7 * distributed in the NASM archive.
8 */
10 #include "compiler.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <inttypes.h>
18 #include "nasm.h"
19 #include "nasmlib.h"
20 #include "saa.h"
21 #include "raa.h"
22 #include "stdscan.h"
23 #include "output/outform.h"
24 #include "output/outlib.h"
25 #include "rbtree.h"
27 #include "output/elf32.h"
28 #include "output/dwarf.h"
29 #include "output/outelf.h"
31 #ifdef OF_ELF32
34 * Relocation types.
36 struct Reloc {
37 struct Reloc *next;
38 int32_t address; /* relative to _start_ of section */
39 int32_t symbol; /* symbol index */
40 int type; /* type of relocation */
43 struct Symbol {
44 struct rbtree symv; /* symbol value and symbol rbtree */
45 int32_t strpos; /* string table position of name */
46 int32_t section; /* section ID of the symbol */
47 int type; /* symbol type */
48 int other; /* symbol visibility */
49 int32_t size; /* size of symbol */
50 int32_t globnum; /* symbol table offset if global */
51 struct Symbol *nextfwd; /* list of unresolved-size symbols */
52 char *name; /* used temporarily if in above list */
55 struct Section {
56 struct SAA *data;
57 uint32_t len, size, nrelocs;
58 int32_t index;
59 int type; /* SHT_PROGBITS or SHT_NOBITS */
60 uint32_t align; /* alignment: power of two */
61 uint32_t flags; /* section flags */
62 char *name;
63 struct SAA *rel;
64 int32_t rellen;
65 struct Reloc *head, **tail;
66 struct rbtree *gsyms; /* global symbols in section */
69 #define SECT_DELTA 32
70 static struct Section **sects;
71 static int nsects, sectlen;
73 #define SHSTR_DELTA 256
74 static char *shstrtab;
75 static int shstrtablen, shstrtabsize;
77 static struct SAA *syms;
78 static uint32_t nlocals, nglobs, ndebugs; /* Symbol counts */
80 static int32_t def_seg;
82 static struct RAA *bsym;
84 static struct SAA *strs;
85 static uint32_t strslen;
87 static FILE *elffp;
88 static efunc error;
89 static evalfunc evaluate;
91 static struct Symbol *fwds;
93 static char elf_module[FILENAME_MAX];
95 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
96 static uint8_t elf_abiver = 0; /* Current ABI version */
98 extern struct ofmt of_elf32;
99 extern struct ofmt of_elf;
101 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
103 static struct ELF_SECTDATA {
104 void *data;
105 int32_t len;
106 bool is_saa;
107 } *elf_sects;
108 static int elf_nsect, nsections;
109 static int32_t elf_foffs;
111 static void elf_write(void);
112 static void elf_sect_write(struct Section *, const uint8_t *,
113 uint32_t);
114 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
115 int, int);
116 static void elf_write_sections(void);
117 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
118 static struct SAA *elf_build_reltab(int32_t *, struct Reloc *);
119 static void add_sectname(char *, char *);
121 struct stabentry {
122 uint32_t n_strx;
123 uint8_t n_type;
124 uint8_t n_other;
125 uint16_t n_desc;
126 uint32_t n_value;
129 struct erel {
130 int offset, info;
133 struct symlininfo {
134 int offset;
135 int section; /* section index */
136 char *name; /* shallow-copied pointer of section name */
139 struct linelist {
140 struct symlininfo info;
141 int line;
142 char *filename;
143 struct linelist *next;
144 struct linelist *last;
147 struct sectlist {
148 struct SAA *psaa;
149 int section;
150 int line;
151 int offset;
152 int file;
153 struct sectlist *next;
154 struct sectlist *last;
157 /* common debug variables */
158 static int currentline = 1;
159 static int debug_immcall = 0;
161 /* stabs debug variables */
162 static struct linelist *stabslines = 0;
163 static int numlinestabs = 0;
164 static char *stabs_filename = 0;
165 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
166 static int stablen, stabstrlen, stabrellen;
168 /* dwarf debug variables */
169 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
170 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
171 static int dwarf_numfiles = 0, dwarf_nsections;
172 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
173 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
174 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
175 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
176 abbrevlen, linelen, linerellen, framelen, loclen;
177 static int32_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
179 static struct dfmt df_dwarf;
180 static struct dfmt df_stabs;
181 static struct Symbol *lastsym;
183 /* common debugging routines */
184 static void debug32_typevalue(int32_t);
185 static void debug32_deflabel(char *, int32_t, int64_t, int, char *);
186 static void debug32_directive(const char *, const char *);
188 /* stabs debugging routines */
189 static void stabs32_linenum(const char *filename, int32_t linenumber, int32_t);
190 static void stabs32_output(int, void *);
191 static void stabs32_generate(void);
192 static void stabs32_cleanup(void);
194 /* dwarf debugging routines */
195 static void dwarf32_init(struct ofmt *, void *, FILE *, efunc);
196 static void dwarf32_linenum(const char *filename, int32_t linenumber, int32_t);
197 static void dwarf32_output(int, void *);
198 static void dwarf32_generate(void);
199 static void dwarf32_cleanup(void);
200 static void dwarf32_findfile(const char *);
201 static void dwarf32_findsect(const int);
204 * Special NASM section numbers which are used to define ELF special
205 * symbols, which can be used with WRT to provide PIC and TLS
206 * relocation types.
208 static int32_t elf_gotpc_sect, elf_gotoff_sect;
209 static int32_t elf_got_sect, elf_plt_sect;
210 static int32_t elf_sym_sect, elf_tlsie_sect;
212 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
214 elffp = fp;
215 error = errfunc;
216 evaluate = eval;
217 (void)ldef; /* placate optimisers */
218 sects = NULL;
219 nsects = sectlen = 0;
220 syms = saa_init((int32_t)sizeof(struct Symbol));
221 nlocals = nglobs = ndebugs = 0;
222 bsym = raa_init();
223 strs = saa_init(1L);
224 saa_wbytes(strs, "\0", 1L);
225 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
226 strslen = 2 + strlen(elf_module);
227 shstrtab = NULL;
228 shstrtablen = shstrtabsize = 0;;
229 add_sectname("", "");
231 fwds = NULL;
233 elf_gotpc_sect = seg_alloc();
234 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf32,
235 error);
236 elf_gotoff_sect = seg_alloc();
237 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf32,
238 error);
239 elf_got_sect = seg_alloc();
240 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf32,
241 error);
242 elf_plt_sect = seg_alloc();
243 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf32,
244 error);
245 elf_sym_sect = seg_alloc();
246 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf32,
247 error);
248 elf_tlsie_sect = seg_alloc();
249 ldef("..tlsie", elf_tlsie_sect + 1, 0L, NULL, false, false, &of_elf32,
250 error);
252 def_seg = seg_alloc();
255 static void elf_init_hack(FILE * fp, efunc errfunc, ldfunc ldef,
256 evalfunc eval)
258 of_elf32.current_dfmt = of_elf.current_dfmt; /* Sync debugging format */
259 elf_init(fp, errfunc, ldef, eval);
262 static void elf_cleanup(int debuginfo)
264 struct Reloc *r;
265 int i;
267 (void)debuginfo;
269 elf_write();
270 fclose(elffp);
271 for (i = 0; i < nsects; i++) {
272 if (sects[i]->type != SHT_NOBITS)
273 saa_free(sects[i]->data);
274 if (sects[i]->head)
275 saa_free(sects[i]->rel);
276 while (sects[i]->head) {
277 r = sects[i]->head;
278 sects[i]->head = sects[i]->head->next;
279 nasm_free(r);
282 nasm_free(sects);
283 saa_free(syms);
284 raa_free(bsym);
285 saa_free(strs);
286 if (of_elf32.current_dfmt) {
287 of_elf32.current_dfmt->cleanup();
291 static void add_sectname(char *firsthalf, char *secondhalf)
293 int len = strlen(firsthalf) + strlen(secondhalf);
294 while (shstrtablen + len + 1 > shstrtabsize)
295 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
296 strcpy(shstrtab + shstrtablen, firsthalf);
297 strcat(shstrtab + shstrtablen, secondhalf);
298 shstrtablen += len + 1;
301 static int elf_make_section(char *name, int type, int flags, int align)
303 struct Section *s;
305 s = nasm_malloc(sizeof(*s));
307 if (type != SHT_NOBITS)
308 s->data = saa_init(1L);
309 s->head = NULL;
310 s->tail = &s->head;
311 s->len = s->size = 0;
312 s->nrelocs = 0;
313 if (!strcmp(name, ".text"))
314 s->index = def_seg;
315 else
316 s->index = seg_alloc();
317 add_sectname("", name);
318 s->name = nasm_malloc(1 + strlen(name));
319 strcpy(s->name, name);
320 s->type = type;
321 s->flags = flags;
322 s->align = align;
323 s->gsyms = NULL;
325 if (nsects >= sectlen)
326 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
327 sects[nsects++] = s;
329 return nsects - 1;
333 static int32_t elf_section_names(char *name, int pass, int *bits)
335 char *p;
336 uint32_t flags, flags_and, flags_or;
337 uint32_t align;
338 int type, i;
341 * Default is 32 bits.
343 if (!name) {
344 *bits = 32;
345 return def_seg;
348 p = name;
349 while (*p && !nasm_isspace(*p))
350 p++;
351 if (*p)
352 *p++ = '\0';
353 flags_and = flags_or = type = align = 0;
355 while (*p && nasm_isspace(*p))
356 p++;
357 while (*p) {
358 char *q = p;
359 while (*p && !nasm_isspace(*p))
360 p++;
361 if (*p)
362 *p++ = '\0';
363 while (*p && nasm_isspace(*p))
364 p++;
366 if (!nasm_strnicmp(q, "align=", 6)) {
367 align = atoi(q + 6);
368 if (align == 0)
369 align = 1;
370 if ((align - 1) & align) { /* means it's not a power of two */
371 error(ERR_NONFATAL, "section alignment %d is not"
372 " a power of two", align);
373 align = 1;
375 } else if (!nasm_stricmp(q, "alloc")) {
376 flags_and |= SHF_ALLOC;
377 flags_or |= SHF_ALLOC;
378 } else if (!nasm_stricmp(q, "noalloc")) {
379 flags_and |= SHF_ALLOC;
380 flags_or &= ~SHF_ALLOC;
381 } else if (!nasm_stricmp(q, "exec")) {
382 flags_and |= SHF_EXECINSTR;
383 flags_or |= SHF_EXECINSTR;
384 } else if (!nasm_stricmp(q, "noexec")) {
385 flags_and |= SHF_EXECINSTR;
386 flags_or &= ~SHF_EXECINSTR;
387 } else if (!nasm_stricmp(q, "write")) {
388 flags_and |= SHF_WRITE;
389 flags_or |= SHF_WRITE;
390 } else if (!nasm_stricmp(q, "tls")) {
391 flags_and |= SHF_TLS;
392 flags_or |= SHF_TLS;
393 } else if (!nasm_stricmp(q, "nowrite")) {
394 flags_and |= SHF_WRITE;
395 flags_or &= ~SHF_WRITE;
396 } else if (!nasm_stricmp(q, "progbits")) {
397 type = SHT_PROGBITS;
398 } else if (!nasm_stricmp(q, "nobits")) {
399 type = SHT_NOBITS;
400 } else if (pass == 1) {
401 error(ERR_WARNING, "Unknown section attribute '%s' ignored on"
402 " declaration of section `%s'", q, name);
406 if (!strcmp(name, ".shstrtab") ||
407 !strcmp(name, ".symtab") ||
408 !strcmp(name, ".strtab")) {
409 error(ERR_NONFATAL, "attempt to redefine reserved section"
410 "name `%s'", name);
411 return NO_SEG;
414 for (i = 0; i < nsects; i++)
415 if (!strcmp(name, sects[i]->name))
416 break;
417 if (i == nsects) {
418 const struct elf_known_section *ks = elf_known_sections;
420 while (ks->name) {
421 if (!strcmp(name, ks->name))
422 break;
423 ks++;
426 type = type ? type : ks->type;
427 align = align ? align : ks->align;
428 flags = (ks->flags & ~flags_and) | flags_or;
430 i = elf_make_section(name, type, flags, align);
431 } else if (pass == 1) {
432 if ((type && sects[i]->type != type)
433 || (align && sects[i]->align != align)
434 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
435 error(ERR_WARNING, "section attributes ignored on"
436 " redeclaration of section `%s'", name);
439 return sects[i]->index;
442 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
443 int is_global, char *special)
445 int pos = strslen;
446 struct Symbol *sym;
447 bool special_used = false;
449 #if defined(DEBUG) && DEBUG>2
450 fprintf(stderr,
451 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
452 name, segment, offset, is_global, special);
453 #endif
454 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
456 * This is a NASM special symbol. We never allow it into
457 * the ELF symbol table, even if it's a valid one. If it
458 * _isn't_ a valid one, we should barf immediately.
460 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
461 strcmp(name, "..got") && strcmp(name, "..plt") &&
462 strcmp(name, "..sym") && strcmp(name, "..tlsie"))
463 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
464 return;
467 if (is_global == 3) {
468 struct Symbol **s;
470 * Fix up a forward-reference symbol size from the first
471 * pass.
473 for (s = &fwds; *s; s = &(*s)->nextfwd)
474 if (!strcmp((*s)->name, name)) {
475 struct tokenval tokval;
476 expr *e;
477 char *p = special;
479 while (*p && !nasm_isspace(*p))
480 p++;
481 while (*p && nasm_isspace(*p))
482 p++;
483 stdscan_reset();
484 stdscan_bufptr = p;
485 tokval.t_type = TOKEN_INVALID;
486 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
487 if (e) {
488 if (!is_simple(e))
489 error(ERR_NONFATAL, "cannot use relocatable"
490 " expression as symbol size");
491 else
492 (*s)->size = reloc_value(e);
496 * Remove it from the list of unresolved sizes.
498 nasm_free((*s)->name);
499 *s = (*s)->nextfwd;
500 return;
502 return; /* it wasn't an important one */
505 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
506 strslen += 1 + strlen(name);
508 lastsym = sym = saa_wstruct(syms);
510 memset(&sym->symv, 0, sizeof(struct rbtree));
512 sym->strpos = pos;
513 sym->type = is_global ? SYM_GLOBAL : 0;
514 sym->other = STV_DEFAULT;
515 sym->size = 0;
516 if (segment == NO_SEG)
517 sym->section = SHN_ABS;
518 else {
519 int i;
520 sym->section = SHN_UNDEF;
521 if (nsects == 0 && segment == def_seg) {
522 int tempint;
523 if (segment != elf_section_names(".text", 2, &tempint))
524 error(ERR_PANIC,
525 "strange segment conditions in ELF driver");
526 sym->section = nsects;
527 } else {
528 for (i = 0; i < nsects; i++)
529 if (segment == sects[i]->index) {
530 sym->section = i + 1;
531 break;
536 if (is_global == 2) {
537 sym->size = offset;
538 sym->symv.key = 0;
539 sym->section = SHN_COMMON;
541 * We have a common variable. Check the special text to see
542 * if it's a valid number and power of two; if so, store it
543 * as the alignment for the common variable.
545 if (special) {
546 bool err;
547 sym->symv.key = readnum(special, &err);
548 if (err)
549 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
550 " valid number", special);
551 else if ((sym->symv.key | (sym->symv.key - 1))
552 != 2 * sym->symv.key - 1)
553 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
554 " power of two", special);
556 special_used = true;
557 } else
558 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
560 if (sym->type == SYM_GLOBAL) {
562 * If sym->section == SHN_ABS, then the first line of the
563 * else section would cause a core dump, because its a reference
564 * beyond the end of the section array.
565 * This behaviour is exhibited by this code:
566 * GLOBAL crash_nasm
567 * crash_nasm equ 0
568 * To avoid such a crash, such requests are silently discarded.
569 * This may not be the best solution.
571 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
572 bsym = raa_write(bsym, segment, nglobs);
573 } else if (sym->section != SHN_ABS) {
575 * This is a global symbol; so we must add it to the rbtree
576 * of global symbols in its section.
578 * In addition, we check the special text for symbol
579 * type and size information.
581 sects[sym->section-1]->gsyms =
582 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
584 if (special) {
585 int n = strcspn(special, " \t");
587 if (!nasm_strnicmp(special, "function", n))
588 sym->type |= STT_FUNC;
589 else if (!nasm_strnicmp(special, "data", n) ||
590 !nasm_strnicmp(special, "object", n))
591 sym->type |= STT_OBJECT;
592 else if (!nasm_strnicmp(special, "notype", n))
593 sym->type |= STT_NOTYPE;
594 else
595 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
596 n, special);
597 special += n;
599 while (nasm_isspace(*special))
600 ++special;
601 if (*special) {
602 n = strcspn(special, " \t");
603 if (!nasm_strnicmp(special, "default", n))
604 sym->other = STV_DEFAULT;
605 else if (!nasm_strnicmp(special, "internal", n))
606 sym->other = STV_INTERNAL;
607 else if (!nasm_strnicmp(special, "hidden", n))
608 sym->other = STV_HIDDEN;
609 else if (!nasm_strnicmp(special, "protected", n))
610 sym->other = STV_PROTECTED;
611 else
612 n = 0;
613 special += n;
616 if (*special) {
617 struct tokenval tokval;
618 expr *e;
619 int fwd = 0;
620 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
622 while (special[n] && nasm_isspace(special[n]))
623 n++;
625 * We have a size expression; attempt to
626 * evaluate it.
628 stdscan_reset();
629 stdscan_bufptr = special + n;
630 tokval.t_type = TOKEN_INVALID;
631 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
632 NULL);
633 if (fwd) {
634 sym->nextfwd = fwds;
635 fwds = sym;
636 sym->name = nasm_strdup(name);
637 } else if (e) {
638 if (!is_simple(e))
639 error(ERR_NONFATAL, "cannot use relocatable"
640 " expression as symbol size");
641 else
642 sym->size = reloc_value(e);
644 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
646 special_used = true;
649 * If TLS segment, mark symbol accordingly.
651 if (sects[sym->section - 1]->flags & SHF_TLS) {
652 sym->type &= 0xf0;
653 sym->type |= STT_TLS;
656 sym->globnum = nglobs;
657 nglobs++;
658 } else
659 nlocals++;
661 if (special && !special_used)
662 error(ERR_NONFATAL, "no special symbol features supported here");
665 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
667 struct Reloc *r;
669 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
670 sect->tail = &r->next;
671 r->next = NULL;
673 r->address = sect->len;
674 if (segment == NO_SEG)
675 r->symbol = 0;
676 else {
677 int i;
678 r->symbol = 0;
679 for (i = 0; i < nsects; i++)
680 if (segment == sects[i]->index)
681 r->symbol = i + 2;
682 if (!r->symbol)
683 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
685 r->type = type;
687 sect->nrelocs++;
691 * This routine deals with ..got and ..sym relocations: the more
692 * complicated kinds. In shared-library writing, some relocations
693 * with respect to global symbols must refer to the precise symbol
694 * rather than referring to an offset from the base of the section
695 * _containing_ the symbol. Such relocations call to this routine,
696 * which searches the symbol list for the symbol in question.
698 * R_386_GOT32 references require the _exact_ symbol address to be
699 * used; R_386_32 references can be at an offset from the symbol.
700 * The boolean argument `exact' tells us this.
702 * Return value is the adjusted value of `addr', having become an
703 * offset from the symbol rather than the section. Should always be
704 * zero when returning from an exact call.
706 * Limitation: if you define two symbols at the same place,
707 * confusion will occur.
709 * Inefficiency: we search, currently, using a linked list which
710 * isn't even necessarily sorted.
712 static int32_t elf_add_gsym_reloc(struct Section *sect,
713 int32_t segment, uint32_t offset,
714 int type, bool exact)
716 struct Reloc *r;
717 struct Section *s;
718 struct Symbol *sym;
719 struct rbtree *srb;
720 int i;
723 * First look up the segment/offset pair and find a global
724 * symbol corresponding to it. If it's not one of our segments,
725 * then it must be an external symbol, in which case we're fine
726 * doing a normal elf_add_reloc after first sanity-checking
727 * that the offset from the symbol is zero.
729 s = NULL;
730 for (i = 0; i < nsects; i++)
731 if (segment == sects[i]->index) {
732 s = sects[i];
733 break;
735 if (!s) {
736 if (exact && offset != 0)
737 error(ERR_NONFATAL, "unable to find a suitable global symbol"
738 " for this reference");
739 else
740 elf_add_reloc(sect, segment, type);
741 return offset;
744 srb = rb_search(s->gsyms, offset);
745 if (!srb || (exact && srb->key != offset)) {
746 error(ERR_NONFATAL, "unable to find a suitable global symbol"
747 " for this reference");
748 return 0;
750 sym = container_of(srb, struct Symbol, symv);
752 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
753 sect->tail = &r->next;
754 r->next = NULL;
756 r->address = sect->len;
757 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
758 r->type = type;
760 sect->nrelocs++;
762 return offset - sym->symv.key;
765 static void elf_out(int32_t segto, const void *data,
766 enum out_type type, uint64_t size,
767 int32_t segment, int32_t wrt)
769 struct Section *s;
770 int32_t addr;
771 uint8_t mydata[4], *p;
772 int i;
773 static struct symlininfo sinfo;
776 * handle absolute-assembly (structure definitions)
778 if (segto == NO_SEG) {
779 if (type != OUT_RESERVE)
780 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
781 " space");
782 return;
785 s = NULL;
786 for (i = 0; i < nsects; i++)
787 if (segto == sects[i]->index) {
788 s = sects[i];
789 break;
791 if (!s) {
792 int tempint; /* ignored */
793 if (segto != elf_section_names(".text", 2, &tempint))
794 error(ERR_PANIC, "strange segment conditions in ELF driver");
795 else {
796 s = sects[nsects - 1];
797 i = nsects - 1;
801 /* again some stabs debugging stuff */
802 if (of_elf32.current_dfmt) {
803 sinfo.offset = s->len;
804 sinfo.section = i;
805 sinfo.name = s->name;
806 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
808 /* end of debugging stuff */
810 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
811 error(ERR_WARNING, "attempt to initialize memory in"
812 " BSS section `%s': ignored", s->name);
813 s->len += realsize(type, size);
814 return;
817 if (type == OUT_RESERVE) {
818 if (s->type == SHT_PROGBITS) {
819 error(ERR_WARNING, "uninitialized space declared in"
820 " non-BSS section `%s': zeroing", s->name);
821 elf_sect_write(s, NULL, size);
822 } else
823 s->len += size;
824 } else if (type == OUT_RAWDATA) {
825 if (segment != NO_SEG)
826 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
827 elf_sect_write(s, data, size);
828 } else if (type == OUT_ADDRESS) {
829 bool gnu16 = false;
830 addr = *(int64_t *)data;
831 if (segment != NO_SEG) {
832 if (segment % 2) {
833 error(ERR_NONFATAL, "ELF format does not support"
834 " segment base references");
835 } else {
836 if (wrt == NO_SEG) {
837 if (size == 2) {
838 gnu16 = true;
839 elf_add_reloc(s, segment, R_386_16);
840 } else {
841 elf_add_reloc(s, segment, R_386_32);
843 } else if (wrt == elf_gotpc_sect + 1) {
845 * The user will supply GOT relative to $$. ELF
846 * will let us have GOT relative to $. So we
847 * need to fix up the data item by $-$$.
849 addr += s->len;
850 elf_add_reloc(s, segment, R_386_GOTPC);
851 } else if (wrt == elf_gotoff_sect + 1) {
852 elf_add_reloc(s, segment, R_386_GOTOFF);
853 } else if (wrt == elf_tlsie_sect + 1) {
854 addr = elf_add_gsym_reloc(s, segment, addr,
855 R_386_TLS_IE, true);
856 } else if (wrt == elf_got_sect + 1) {
857 addr = elf_add_gsym_reloc(s, segment, addr,
858 R_386_GOT32, true);
859 } else if (wrt == elf_sym_sect + 1) {
860 if (size == 2) {
861 gnu16 = true;
862 addr = elf_add_gsym_reloc(s, segment, addr,
863 R_386_16, false);
864 } else {
865 addr = elf_add_gsym_reloc(s, segment, addr,
866 R_386_32, false);
868 } else if (wrt == elf_plt_sect + 1) {
869 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
870 "relative PLT references");
871 } else {
872 error(ERR_NONFATAL, "ELF format does not support this"
873 " use of WRT");
874 wrt = NO_SEG; /* we can at least _try_ to continue */
878 p = mydata;
879 if (gnu16) {
880 error(ERR_WARNING | ERR_WARN_GNUELF,
881 "16-bit relocations in ELF is a GNU extension");
882 WRITESHORT(p, addr);
883 } else {
884 if (size != 4 && segment != NO_SEG) {
885 error(ERR_NONFATAL,
886 "Unsupported non-32-bit ELF relocation");
888 WRITELONG(p, addr);
890 elf_sect_write(s, mydata, size);
891 } else if (type == OUT_REL2ADR) {
892 if (segment == segto)
893 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
894 if (segment != NO_SEG && segment % 2) {
895 error(ERR_NONFATAL, "ELF format does not support"
896 " segment base references");
897 } else {
898 if (wrt == NO_SEG) {
899 error(ERR_WARNING | ERR_WARN_GNUELF,
900 "16-bit relocations in ELF is a GNU extension");
901 elf_add_reloc(s, segment, R_386_PC16);
902 } else {
903 error(ERR_NONFATAL,
904 "Unsupported non-32-bit ELF relocation");
907 p = mydata;
908 WRITESHORT(p, *(int64_t *)data - size);
909 elf_sect_write(s, mydata, 2L);
910 } else if (type == OUT_REL4ADR) {
911 if (segment == segto)
912 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
913 if (segment != NO_SEG && segment % 2) {
914 error(ERR_NONFATAL, "ELF format does not support"
915 " segment base references");
916 } else {
917 if (wrt == NO_SEG) {
918 elf_add_reloc(s, segment, R_386_PC32);
919 } else if (wrt == elf_plt_sect + 1) {
920 elf_add_reloc(s, segment, R_386_PLT32);
921 } else if (wrt == elf_gotpc_sect + 1 ||
922 wrt == elf_gotoff_sect + 1 ||
923 wrt == elf_got_sect + 1) {
924 error(ERR_NONFATAL, "ELF format cannot produce PC-"
925 "relative GOT references");
926 } else {
927 error(ERR_NONFATAL, "ELF format does not support this"
928 " use of WRT");
929 wrt = NO_SEG; /* we can at least _try_ to continue */
932 p = mydata;
933 WRITELONG(p, *(int64_t *)data - size);
934 elf_sect_write(s, mydata, 4L);
938 static void elf_write(void)
940 int align;
941 char *p;
942 int i;
944 struct SAA *symtab;
945 int32_t symtablen, symtablocal;
948 * Work out how many sections we will have. We have SHN_UNDEF,
949 * then the flexible user sections, then the fixed sections
950 * `.shstrtab', `.symtab' and `.strtab', then optionally
951 * relocation sections for the user sections.
953 nsections = sec_numspecial + 1;
954 if (of_elf32.current_dfmt == &df_stabs)
955 nsections += 3;
956 else if (of_elf32.current_dfmt == &df_dwarf)
957 nsections += 10;
959 add_sectname("", ".shstrtab");
960 add_sectname("", ".symtab");
961 add_sectname("", ".strtab");
962 for (i = 0; i < nsects; i++) {
963 nsections++; /* for the section itself */
964 if (sects[i]->head) {
965 nsections++; /* for its relocations */
966 add_sectname(".rel", sects[i]->name);
970 if (of_elf32.current_dfmt == &df_stabs) {
971 /* in case the debug information is wanted, just add these three sections... */
972 add_sectname("", ".stab");
973 add_sectname("", ".stabstr");
974 add_sectname(".rel", ".stab");
975 } else if (of_elf32.current_dfmt == &df_dwarf) {
976 /* the dwarf debug standard specifies the following ten sections,
977 not all of which are currently implemented,
978 although all of them are defined. */
979 add_sectname("", ".debug_aranges");
980 add_sectname(".rela", ".debug_aranges");
981 add_sectname("", ".debug_pubnames");
982 add_sectname("", ".debug_info");
983 add_sectname(".rela", ".debug_info");
984 add_sectname("", ".debug_abbrev");
985 add_sectname("", ".debug_line");
986 add_sectname(".rela", ".debug_line");
987 add_sectname("", ".debug_frame");
988 add_sectname("", ".debug_loc");
992 * Output the ELF header.
994 fwrite("\177ELF\1\1\1", 7, 1, elffp);
995 fputc(elf_osabi, elffp);
996 fputc(elf_abiver, elffp);
997 fwritezero(7, elffp);
998 fwriteint16_t(1, elffp); /* ET_REL relocatable file */
999 fwriteint16_t(3, elffp); /* EM_386 processor ID */
1000 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1001 fwriteint32_t(0L, elffp); /* no entry point */
1002 fwriteint32_t(0L, elffp); /* no program header table */
1003 fwriteint32_t(0x40L, elffp); /* section headers straight after
1004 * ELF header plus alignment */
1005 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1006 fwriteint16_t(0x34, elffp); /* size of ELF header */
1007 fwriteint16_t(0, elffp); /* no program header table, again */
1008 fwriteint16_t(0, elffp); /* still no program header table */
1009 fwriteint16_t(0x28, elffp); /* size of section header */
1010 fwriteint16_t(nsections, elffp); /* number of sections */
1011 fwriteint16_t(sec_shstrtab, elffp); /* string table section index for
1012 * section header table */
1013 fwriteint32_t(0L, elffp); /* align to 0x40 bytes */
1014 fwriteint32_t(0L, elffp);
1015 fwriteint32_t(0L, elffp);
1018 * Build the symbol table and relocation tables.
1020 symtab = elf_build_symtab(&symtablen, &symtablocal);
1021 for (i = 0; i < nsects; i++)
1022 if (sects[i]->head)
1023 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1024 sects[i]->head);
1027 * Now output the section header table.
1030 elf_foffs = 0x40 + 0x28 * nsections;
1031 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1032 elf_foffs += align;
1033 elf_nsect = 0;
1034 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1036 /* SHN_UNDEF */
1037 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1038 p = shstrtab + 1;
1040 /* The normal sections */
1041 for (i = 0; i < nsects; i++) {
1042 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1043 (sects[i]->type == SHT_PROGBITS ?
1044 sects[i]->data : NULL), true,
1045 sects[i]->len, 0, 0, sects[i]->align, 0);
1046 p += strlen(p) + 1;
1049 /* .shstrtab */
1050 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1051 shstrtablen, 0, 0, 1, 0);
1052 p += strlen(p) + 1;
1054 /* .symtab */
1055 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1056 symtablen, sec_strtab, symtablocal, 4, 16);
1057 p += strlen(p) + 1;
1059 /* .strtab */
1060 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1061 strslen, 0, 0, 1, 0);
1062 p += strlen(p) + 1;
1064 /* The relocation sections */
1065 for (i = 0; i < nsects; i++)
1066 if (sects[i]->head) {
1067 elf_section_header(p - shstrtab, SHT_REL, 0, sects[i]->rel, true,
1068 sects[i]->rellen, sec_symtab, i + 1, 4, 8);
1069 p += strlen(p) + 1;
1073 if (of_elf32.current_dfmt == &df_stabs) {
1074 /* for debugging information, create the last three sections
1075 which are the .stab , .stabstr and .rel.stab sections respectively */
1077 /* this function call creates the stab sections in memory */
1078 stabs32_generate();
1080 if (stabbuf && stabstrbuf && stabrelbuf) {
1081 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1082 stablen, sec_stabstr, 0, 4, 12);
1083 p += strlen(p) + 1;
1085 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1086 stabstrlen, 0, 0, 4, 0);
1087 p += strlen(p) + 1;
1089 /* link -> symtable info -> section to refer to */
1090 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1091 stabrellen, sec_symtab, sec_stab, 4, 8);
1092 p += strlen(p) + 1;
1094 } else if (of_elf32.current_dfmt == &df_dwarf) {
1095 /* for dwarf debugging information, create the ten dwarf sections */
1097 /* this function call creates the dwarf sections in memory */
1098 if (dwarf_fsect)
1099 dwarf32_generate();
1101 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1102 arangeslen, 0, 0, 1, 0);
1103 p += strlen(p) + 1;
1105 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1106 arangesrellen, sec_symtab, sec_debug_aranges,
1107 1, 12);
1108 p += strlen(p) + 1;
1110 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf,
1111 false, pubnameslen, 0, 0, 1, 0);
1112 p += strlen(p) + 1;
1114 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1115 infolen, 0, 0, 1, 0);
1116 p += strlen(p) + 1;
1118 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1119 inforellen, sec_symtab, sec_debug_info, 1, 12);
1120 p += strlen(p) + 1;
1122 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1123 abbrevlen, 0, 0, 1, 0);
1124 p += strlen(p) + 1;
1126 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1127 linelen, 0, 0, 1, 0);
1128 p += strlen(p) + 1;
1130 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1131 linerellen, sec_symtab, sec_debug_line, 1, 12);
1132 p += strlen(p) + 1;
1134 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1135 framelen, 0, 0, 8, 0);
1136 p += strlen(p) + 1;
1138 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1139 loclen, 0, 0, 1, 0);
1140 p += strlen(p) + 1;
1142 fwritezero(align, elffp);
1145 * Now output the sections.
1147 elf_write_sections();
1149 nasm_free(elf_sects);
1150 saa_free(symtab);
1153 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1155 struct SAA *s = saa_init(1L);
1156 struct Symbol *sym;
1157 uint8_t entry[16], *p;
1158 int i;
1160 *len = *local = 0;
1163 * First, an all-zeros entry, required by the ELF spec.
1165 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1166 *len += 16;
1167 (*local)++;
1170 * Next, an entry for the file name.
1172 p = entry;
1173 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1174 WRITELONG(p, 0); /* no value */
1175 WRITELONG(p, 0); /* no size either */
1176 WRITESHORT(p, STT_FILE); /* type FILE */
1177 WRITESHORT(p, SHN_ABS);
1178 saa_wbytes(s, entry, 16L);
1179 *len += 16;
1180 (*local)++;
1183 * Now some standard symbols defining the segments, for relocation
1184 * purposes.
1186 for (i = 1; i <= nsects; i++) {
1187 p = entry;
1188 WRITELONG(p, 0); /* no symbol name */
1189 WRITELONG(p, 0); /* offset zero */
1190 WRITELONG(p, 0); /* size zero */
1191 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1192 WRITESHORT(p, i); /* section id */
1193 saa_wbytes(s, entry, 16L);
1194 *len += 16;
1195 (*local)++;
1199 * Now the other local symbols.
1201 saa_rewind(syms);
1202 while ((sym = saa_rstruct(syms))) {
1203 if (sym->type & SYM_GLOBAL)
1204 continue;
1205 p = entry;
1206 WRITELONG(p, sym->strpos);
1207 WRITELONG(p, sym->symv.key);
1208 WRITELONG(p, sym->size);
1209 WRITECHAR(p, sym->type); /* type and binding */
1210 WRITECHAR(p, sym->other); /* visibility */
1211 WRITESHORT(p, sym->section);
1212 saa_wbytes(s, entry, 16L);
1213 *len += 16;
1214 (*local)++;
1217 * dwarf needs symbols for debug sections
1218 * which are relocation targets.
1220 //*** fix for 32 bit
1221 if (of_elf32.current_dfmt == &df_dwarf) {
1222 dwarf_infosym = *local;
1223 p = entry;
1224 WRITELONG(p, 0); /* no symbol name */
1225 WRITELONG(p, (uint32_t) 0); /* offset zero */
1226 WRITELONG(p, (uint32_t) 0); /* size zero */
1227 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1228 WRITESHORT(p, sec_debug_info); /* section id */
1229 saa_wbytes(s, entry, 16L);
1230 *len += 16;
1231 (*local)++;
1232 dwarf_abbrevsym = *local;
1233 p = entry;
1234 WRITELONG(p, 0); /* no symbol name */
1235 WRITELONG(p, (uint32_t) 0); /* offset zero */
1236 WRITELONG(p, (uint32_t) 0); /* size zero */
1237 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1238 WRITESHORT(p, sec_debug_abbrev); /* section id */
1239 saa_wbytes(s, entry, 16L);
1240 *len += 16;
1241 (*local)++;
1242 dwarf_linesym = *local;
1243 p = entry;
1244 WRITELONG(p, 0); /* no symbol name */
1245 WRITELONG(p, (uint32_t) 0); /* offset zero */
1246 WRITELONG(p, (uint32_t) 0); /* size zero */
1247 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1248 WRITESHORT(p, sec_debug_line); /* section id */
1249 saa_wbytes(s, entry, 16L);
1250 *len += 16;
1251 (*local)++;
1255 * Now the global symbols.
1257 saa_rewind(syms);
1258 while ((sym = saa_rstruct(syms))) {
1259 if (!(sym->type & SYM_GLOBAL))
1260 continue;
1261 p = entry;
1262 WRITELONG(p, sym->strpos);
1263 WRITELONG(p, sym->symv.key);
1264 WRITELONG(p, sym->size);
1265 WRITECHAR(p, sym->type); /* type and binding */
1266 WRITECHAR(p, sym->other); /* visibility */
1267 WRITESHORT(p, sym->section);
1268 saa_wbytes(s, entry, 16L);
1269 *len += 16;
1272 return s;
1275 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1277 struct SAA *s;
1278 uint8_t *p, entry[8];
1279 int32_t global_offset;
1281 if (!r)
1282 return NULL;
1284 s = saa_init(1L);
1285 *len = 0;
1288 * How to onvert from a global placeholder to a real symbol index;
1289 * the +2 refers to the two special entries, the null entry and
1290 * the filename entry.
1292 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1294 while (r) {
1295 int32_t sym = r->symbol;
1298 * Create a real symbol index; the +2 refers to the two special
1299 * entries, the null entry and the filename entry.
1301 if (sym >= GLOBAL_TEMP_BASE)
1302 sym += global_offset;
1304 p = entry;
1305 WRITELONG(p, r->address);
1306 WRITELONG(p, (sym << 8) + r->type);
1307 saa_wbytes(s, entry, 8L);
1308 *len += 8;
1310 r = r->next;
1313 return s;
1316 static void elf_section_header(int name, int type, int flags,
1317 void *data, bool is_saa, int32_t datalen,
1318 int link, int info, int align, int eltsize)
1320 elf_sects[elf_nsect].data = data;
1321 elf_sects[elf_nsect].len = datalen;
1322 elf_sects[elf_nsect].is_saa = is_saa;
1323 elf_nsect++;
1325 fwriteint32_t((int32_t)name, elffp);
1326 fwriteint32_t((int32_t)type, elffp);
1327 fwriteint32_t((int32_t)flags, elffp);
1328 fwriteint32_t(0L, elffp); /* no address, ever, in object files */
1329 fwriteint32_t(type == 0 ? 0L : elf_foffs, elffp);
1330 fwriteint32_t(datalen, elffp);
1331 if (data)
1332 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1333 fwriteint32_t((int32_t)link, elffp);
1334 fwriteint32_t((int32_t)info, elffp);
1335 fwriteint32_t((int32_t)align, elffp);
1336 fwriteint32_t((int32_t)eltsize, elffp);
1339 static void elf_write_sections(void)
1341 int i;
1342 for (i = 0; i < elf_nsect; i++)
1343 if (elf_sects[i].data) {
1344 int32_t len = elf_sects[i].len;
1345 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1346 int32_t align = reallen - len;
1347 if (elf_sects[i].is_saa)
1348 saa_fpwrite(elf_sects[i].data, elffp);
1349 else
1350 fwrite(elf_sects[i].data, len, 1, elffp);
1351 fwritezero(align, elffp);
1355 static void elf_sect_write(struct Section *sect,
1356 const uint8_t *data, uint32_t len)
1358 saa_wbytes(sect->data, data, len);
1359 sect->len += len;
1362 static int32_t elf_segbase(int32_t segment)
1364 return segment;
1367 static int elf_directive(char *directive, char *value, int pass)
1369 bool err;
1370 int64_t n;
1371 char *p;
1373 if (!strcmp(directive, "osabi")) {
1374 if (pass == 2)
1375 return 1; /* ignore in pass 2 */
1377 n = readnum(value, &err);
1378 if (err) {
1379 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1380 return 1;
1382 if (n < 0 || n > 255) {
1383 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1384 return 1;
1386 elf_osabi = n;
1387 elf_abiver = 0;
1389 if ((p = strchr(value,',')) == NULL)
1390 return 1;
1392 n = readnum(p+1, &err);
1393 if (err || n < 0 || n > 255) {
1394 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1395 return 1;
1398 elf_abiver = n;
1399 return 1;
1402 return 0;
1405 static void elf_filename(char *inname, char *outname, efunc error)
1407 strcpy(elf_module, inname);
1408 standard_extension(inname, outname, ".o", error);
1411 extern macros_t elf_stdmac[];
1413 static int elf_set_info(enum geninfo type, char **val)
1415 (void)type;
1416 (void)val;
1417 return 0;
1419 static struct dfmt df_dwarf = {
1420 "ELF32 (i386) dwarf debug format for Linux/Unix",
1421 "dwarf",
1422 dwarf32_init,
1423 dwarf32_linenum,
1424 debug32_deflabel,
1425 debug32_directive,
1426 debug32_typevalue,
1427 dwarf32_output,
1428 dwarf32_cleanup
1430 static struct dfmt df_stabs = {
1431 "ELF32 (i386) stabs debug format for Linux/Unix",
1432 "stabs",
1433 null_debug_init,
1434 stabs32_linenum,
1435 debug32_deflabel,
1436 debug32_directive,
1437 debug32_typevalue,
1438 stabs32_output,
1439 stabs32_cleanup
1442 struct dfmt *elf32_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1444 struct ofmt of_elf32 = {
1445 "ELF32 (i386) object files (e.g. Linux)",
1446 "elf32",
1447 NULL,
1448 elf32_debugs_arr,
1449 &df_stabs,
1450 elf_stdmac,
1451 elf_init,
1452 elf_set_info,
1453 elf_out,
1454 elf_deflabel,
1455 elf_section_names,
1456 elf_segbase,
1457 elf_directive,
1458 elf_filename,
1459 elf_cleanup
1462 struct ofmt of_elf = {
1463 "ELF (short name for ELF32) ",
1464 "elf",
1465 NULL,
1466 elf32_debugs_arr,
1467 &df_stabs,
1468 elf_stdmac,
1469 elf_init_hack,
1470 elf_set_info,
1471 elf_out,
1472 elf_deflabel,
1473 elf_section_names,
1474 elf_segbase,
1475 elf_directive,
1476 elf_filename,
1477 elf_cleanup
1479 /* again, the stabs debugging stuff (code) */
1481 static void stabs32_linenum(const char *filename, int32_t linenumber,
1482 int32_t segto)
1484 (void)segto;
1486 if (!stabs_filename) {
1487 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1488 strcpy(stabs_filename, filename);
1489 } else {
1490 if (strcmp(stabs_filename, filename)) {
1491 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1492 in fact, this leak comes in quite handy to maintain a list of files
1493 encountered so far in the symbol lines... */
1495 /* why not nasm_free(stabs_filename); we're done with the old one */
1497 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1498 strcpy(stabs_filename, filename);
1501 debug_immcall = 1;
1502 currentline = linenumber;
1505 static void debug32_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1506 char *special)
1508 (void)name;
1509 (void)segment;
1510 (void)offset;
1511 (void)is_global;
1512 (void)special;
1515 static void debug32_directive(const char *directive, const char *params)
1517 (void)directive;
1518 (void)params;
1521 static void debug32_typevalue(int32_t type)
1523 int32_t stype, ssize;
1524 switch (TYM_TYPE(type)) {
1525 case TY_LABEL:
1526 ssize = 0;
1527 stype = STT_NOTYPE;
1528 break;
1529 case TY_BYTE:
1530 ssize = 1;
1531 stype = STT_OBJECT;
1532 break;
1533 case TY_WORD:
1534 ssize = 2;
1535 stype = STT_OBJECT;
1536 break;
1537 case TY_DWORD:
1538 ssize = 4;
1539 stype = STT_OBJECT;
1540 break;
1541 case TY_FLOAT:
1542 ssize = 4;
1543 stype = STT_OBJECT;
1544 break;
1545 case TY_QWORD:
1546 ssize = 8;
1547 stype = STT_OBJECT;
1548 break;
1549 case TY_TBYTE:
1550 ssize = 10;
1551 stype = STT_OBJECT;
1552 break;
1553 case TY_OWORD:
1554 ssize = 8;
1555 stype = STT_OBJECT;
1556 break;
1557 case TY_COMMON:
1558 ssize = 0;
1559 stype = STT_COMMON;
1560 break;
1561 case TY_SEG:
1562 ssize = 0;
1563 stype = STT_SECTION;
1564 break;
1565 case TY_EXTERN:
1566 ssize = 0;
1567 stype = STT_NOTYPE;
1568 break;
1569 case TY_EQU:
1570 ssize = 0;
1571 stype = STT_NOTYPE;
1572 break;
1573 default:
1574 ssize = 0;
1575 stype = STT_NOTYPE;
1576 break;
1578 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1579 lastsym->size = ssize;
1580 lastsym->type = stype;
1584 static void stabs32_output(int type, void *param)
1586 struct symlininfo *s;
1587 struct linelist *el;
1588 if (type == TY_STABSSYMLIN) {
1589 if (debug_immcall) {
1590 s = (struct symlininfo *)param;
1591 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1592 return; /* we are only interested in the text stuff */
1593 numlinestabs++;
1594 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1595 el->info.offset = s->offset;
1596 el->info.section = s->section;
1597 el->info.name = s->name;
1598 el->line = currentline;
1599 el->filename = stabs_filename;
1600 el->next = 0;
1601 if (stabslines) {
1602 stabslines->last->next = el;
1603 stabslines->last = el;
1604 } else {
1605 stabslines = el;
1606 stabslines->last = el;
1610 debug_immcall = 0;
1613 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1614 do {\
1615 WRITELONG(p,n_strx); \
1616 WRITECHAR(p,n_type); \
1617 WRITECHAR(p,n_other); \
1618 WRITESHORT(p,n_desc); \
1619 WRITELONG(p,n_value); \
1620 } while (0)
1622 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1624 static void stabs32_generate(void)
1626 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1627 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1628 char **allfiles;
1629 int *fileidx;
1631 struct linelist *ptr;
1633 ptr = stabslines;
1635 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1636 for (i = 0; i < numlinestabs; i++)
1637 allfiles[i] = 0;
1638 numfiles = 0;
1639 while (ptr) {
1640 if (numfiles == 0) {
1641 allfiles[0] = ptr->filename;
1642 numfiles++;
1643 } else {
1644 for (i = 0; i < numfiles; i++) {
1645 if (!strcmp(allfiles[i], ptr->filename))
1646 break;
1648 if (i >= numfiles) {
1649 allfiles[i] = ptr->filename;
1650 numfiles++;
1653 ptr = ptr->next;
1655 strsize = 1;
1656 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1657 for (i = 0; i < numfiles; i++) {
1658 fileidx[i] = strsize;
1659 strsize += strlen(allfiles[i]) + 1;
1661 mainfileindex = 0;
1662 for (i = 0; i < numfiles; i++) {
1663 if (!strcmp(allfiles[i], elf_module)) {
1664 mainfileindex = i;
1665 break;
1669 /* worst case size of the stab buffer would be:
1670 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1672 sbuf =
1673 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1674 sizeof(struct stabentry));
1676 ssbuf = (uint8_t *)nasm_malloc(strsize);
1678 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1679 rptr = rbuf;
1681 for (i = 0; i < numfiles; i++) {
1682 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1684 ssbuf[0] = 0;
1686 stabstrlen = strsize; /* set global variable for length of stab strings */
1688 sptr = sbuf;
1689 ptr = stabslines;
1690 numstabs = 0;
1692 if (ptr) {
1693 /* this is the first stab, its strx points to the filename of the
1694 the source-file, the n_desc field should be set to the number
1695 of remaining stabs
1697 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1699 /* this is the stab for the main source file */
1700 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1702 /* relocation table entry */
1704 /* Since the symbol table has two entries before */
1705 /* the section symbols, the index in the info.section */
1706 /* member must be adjusted by adding 2 */
1708 WRITELONG(rptr, (sptr - sbuf) - 4);
1709 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1711 numstabs++;
1712 currfile = mainfileindex;
1715 while (ptr) {
1716 if (strcmp(allfiles[currfile], ptr->filename)) {
1717 /* oops file has changed... */
1718 for (i = 0; i < numfiles; i++)
1719 if (!strcmp(allfiles[i], ptr->filename))
1720 break;
1721 currfile = i;
1722 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1723 ptr->info.offset);
1724 numstabs++;
1726 /* relocation table entry */
1727 WRITELONG(rptr, (sptr - sbuf) - 4);
1728 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1731 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1732 numstabs++;
1734 /* relocation table entry */
1736 WRITELONG(rptr, (sptr - sbuf) - 4);
1737 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1739 ptr = ptr->next;
1743 ((struct stabentry *)sbuf)->n_desc = numstabs;
1745 nasm_free(allfiles);
1746 nasm_free(fileidx);
1748 stablen = (sptr - sbuf);
1749 stabrellen = (rptr - rbuf);
1750 stabrelbuf = rbuf;
1751 stabbuf = sbuf;
1752 stabstrbuf = ssbuf;
1755 static void stabs32_cleanup(void)
1757 struct linelist *ptr, *del;
1758 if (!stabslines)
1759 return;
1760 ptr = stabslines;
1761 while (ptr) {
1762 del = ptr;
1763 ptr = ptr->next;
1764 nasm_free(del);
1766 if (stabbuf)
1767 nasm_free(stabbuf);
1768 if (stabrelbuf)
1769 nasm_free(stabrelbuf);
1770 if (stabstrbuf)
1771 nasm_free(stabstrbuf);
1774 /* dwarf routines */
1776 static void dwarf32_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1778 (void)of;
1779 (void)id;
1780 (void)fp;
1781 (void)error;
1783 ndebugs = 3; /* 3 debug symbols */
1786 static void dwarf32_linenum(const char *filename, int32_t linenumber,
1787 int32_t segto)
1789 (void)segto;
1790 dwarf32_findfile(filename);
1791 debug_immcall = 1;
1792 currentline = linenumber;
1795 /* called from elf_out with type == TY_DEBUGSYMLIN */
1796 static void dwarf32_output(int type, void *param)
1798 int ln, aa, inx, maxln, soc;
1799 struct symlininfo *s;
1800 struct SAA *plinep;
1802 (void)type;
1804 s = (struct symlininfo *)param;
1805 /* line number info is only gathered for executable sections */
1806 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1807 return;
1808 /* Check if section index has changed */
1809 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1811 dwarf32_findsect(s->section);
1813 /* do nothing unless line or file has changed */
1814 if (debug_immcall)
1816 ln = currentline - dwarf_csect->line;
1817 aa = s->offset - dwarf_csect->offset;
1818 inx = dwarf_clist->line;
1819 plinep = dwarf_csect->psaa;
1820 /* check for file change */
1821 if (!(inx == dwarf_csect->file))
1823 saa_write8(plinep,DW_LNS_set_file);
1824 saa_write8(plinep,inx);
1825 dwarf_csect->file = inx;
1827 /* check for line change */
1828 if (ln)
1830 /* test if in range of special op code */
1831 maxln = line_base + line_range;
1832 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1833 if (ln >= line_base && ln < maxln && soc < 256)
1835 saa_write8(plinep,soc);
1837 else
1839 if (ln)
1841 saa_write8(plinep,DW_LNS_advance_line);
1842 saa_wleb128s(plinep,ln);
1844 if (aa)
1846 saa_write8(plinep,DW_LNS_advance_pc);
1847 saa_wleb128u(plinep,aa);
1850 dwarf_csect->line = currentline;
1851 dwarf_csect->offset = s->offset;
1853 /* show change handled */
1854 debug_immcall = 0;
1859 static void dwarf32_generate(void)
1861 uint8_t *pbuf;
1862 int indx;
1863 struct linelist *ftentry;
1864 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1865 struct SAA *parangesrel, *plinesrel, *pinforel;
1866 struct sectlist *psect;
1867 size_t saalen, linepoff, totlen, highaddr;
1869 /* write epilogues for each line program range */
1870 /* and build aranges section */
1871 paranges = saa_init(1L);
1872 parangesrel = saa_init(1L);
1873 saa_write16(paranges,2); /* dwarf version */
1874 saa_write32(parangesrel, paranges->datalen+4);
1875 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
1876 saa_write32(parangesrel, 0);
1877 saa_write32(paranges,0); /* offset into info */
1878 saa_write8(paranges,4); /* pointer size */
1879 saa_write8(paranges,0); /* not segmented */
1880 saa_write32(paranges,0); /* padding */
1881 /* iterate though sectlist entries */
1882 psect = dwarf_fsect;
1883 totlen = 0;
1884 highaddr = 0;
1885 for (indx = 0; indx < dwarf_nsections; indx++)
1887 plinep = psect->psaa;
1888 /* Line Number Program Epilogue */
1889 saa_write8(plinep,2); /* std op 2 */
1890 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1891 saa_write8(plinep,DW_LNS_extended_op);
1892 saa_write8(plinep,1); /* operand length */
1893 saa_write8(plinep,DW_LNE_end_sequence);
1894 totlen += plinep->datalen;
1895 /* range table relocation entry */
1896 saa_write32(parangesrel, paranges->datalen + 4);
1897 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
1898 saa_write32(parangesrel, (uint32_t) 0);
1899 /* range table entry */
1900 saa_write32(paranges,0x0000); /* range start */
1901 saa_write32(paranges,sects[psect->section]->len); /* range length */
1902 highaddr += sects[psect->section]->len;
1903 /* done with this entry */
1904 psect = psect->next;
1906 saa_write32(paranges,0); /* null address */
1907 saa_write32(paranges,0); /* null length */
1908 saalen = paranges->datalen;
1909 arangeslen = saalen + 4;
1910 arangesbuf = pbuf = nasm_malloc(arangeslen);
1911 WRITELONG(pbuf,saalen); /* initial length */
1912 saa_rnbytes(paranges, pbuf, saalen);
1913 saa_free(paranges);
1915 /* build rela.aranges section */
1916 arangesrellen = saalen = parangesrel->datalen;
1917 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1918 saa_rnbytes(parangesrel, pbuf, saalen);
1919 saa_free(parangesrel);
1921 /* build pubnames section */
1922 ppubnames = saa_init(1L);
1923 saa_write16(ppubnames,3); /* dwarf version */
1924 saa_write32(ppubnames,0); /* offset into info */
1925 saa_write32(ppubnames,0); /* space used in info */
1926 saa_write32(ppubnames,0); /* end of list */
1927 saalen = ppubnames->datalen;
1928 pubnameslen = saalen + 4;
1929 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1930 WRITELONG(pbuf,saalen); /* initial length */
1931 saa_rnbytes(ppubnames, pbuf, saalen);
1932 saa_free(ppubnames);
1934 /* build info section */
1935 pinfo = saa_init(1L);
1936 pinforel = saa_init(1L);
1937 saa_write16(pinfo,2); /* dwarf version */
1938 saa_write32(pinforel, pinfo->datalen + 4);
1939 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
1940 saa_write32(pinforel, 0);
1941 saa_write32(pinfo,0); /* offset into abbrev */
1942 saa_write8(pinfo,4); /* pointer size */
1943 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1944 saa_write32(pinforel, pinfo->datalen + 4);
1945 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1946 saa_write32(pinforel, 0);
1947 saa_write32(pinfo,0); /* DW_AT_low_pc */
1948 saa_write32(pinforel, pinfo->datalen + 4);
1949 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1950 saa_write32(pinforel, 0);
1951 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
1952 saa_write32(pinforel, pinfo->datalen + 4);
1953 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
1954 saa_write32(pinforel, 0);
1955 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1956 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1957 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1958 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1959 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1960 saa_write32(pinforel, pinfo->datalen + 4);
1961 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1962 saa_write32(pinforel, 0);
1963 saa_write32(pinfo,0); /* DW_AT_low_pc */
1964 saa_write32(pinfo,0); /* DW_AT_frame_base */
1965 saa_write8(pinfo,0); /* end of entries */
1966 saalen = pinfo->datalen;
1967 infolen = saalen + 4;
1968 infobuf = pbuf = nasm_malloc(infolen);
1969 WRITELONG(pbuf,saalen); /* initial length */
1970 saa_rnbytes(pinfo, pbuf, saalen);
1971 saa_free(pinfo);
1973 /* build rela.info section */
1974 inforellen = saalen = pinforel->datalen;
1975 inforelbuf = pbuf = nasm_malloc(inforellen);
1976 saa_rnbytes(pinforel, pbuf, saalen);
1977 saa_free(pinforel);
1979 /* build abbrev section */
1980 pabbrev = saa_init(1L);
1981 saa_write8(pabbrev,1); /* entry number LEB128u */
1982 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
1983 saa_write8(pabbrev,1); /* has children */
1984 /* the following attributes and forms are all LEB128u values */
1985 saa_write8(pabbrev,DW_AT_low_pc);
1986 saa_write8(pabbrev,DW_FORM_addr);
1987 saa_write8(pabbrev,DW_AT_high_pc);
1988 saa_write8(pabbrev,DW_FORM_addr);
1989 saa_write8(pabbrev,DW_AT_stmt_list);
1990 saa_write8(pabbrev,DW_FORM_data4);
1991 saa_write8(pabbrev,DW_AT_name);
1992 saa_write8(pabbrev,DW_FORM_string);
1993 saa_write8(pabbrev,DW_AT_producer);
1994 saa_write8(pabbrev,DW_FORM_string);
1995 saa_write8(pabbrev,DW_AT_language);
1996 saa_write8(pabbrev,DW_FORM_data2);
1997 saa_write16(pabbrev,0); /* end of entry */
1998 /* LEB128u usage same as above */
1999 saa_write8(pabbrev,2); /* entry number */
2000 saa_write8(pabbrev,DW_TAG_subprogram);
2001 saa_write8(pabbrev,0); /* no children */
2002 saa_write8(pabbrev,DW_AT_low_pc);
2003 saa_write8(pabbrev,DW_FORM_addr);
2004 saa_write8(pabbrev,DW_AT_frame_base);
2005 saa_write8(pabbrev,DW_FORM_data4);
2006 saa_write16(pabbrev,0); /* end of entry */
2007 abbrevlen = saalen = pabbrev->datalen;
2008 abbrevbuf = pbuf = nasm_malloc(saalen);
2009 saa_rnbytes(pabbrev, pbuf, saalen);
2010 saa_free(pabbrev);
2012 /* build line section */
2013 /* prolog */
2014 plines = saa_init(1L);
2015 saa_write8(plines,1); /* Minimum Instruction Length */
2016 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2017 saa_write8(plines,line_base); /* Line Base */
2018 saa_write8(plines,line_range); /* Line Range */
2019 saa_write8(plines,opcode_base); /* Opcode Base */
2020 /* standard opcode lengths (# of LEB128u operands) */
2021 saa_write8(plines,0); /* Std opcode 1 length */
2022 saa_write8(plines,1); /* Std opcode 2 length */
2023 saa_write8(plines,1); /* Std opcode 3 length */
2024 saa_write8(plines,1); /* Std opcode 4 length */
2025 saa_write8(plines,1); /* Std opcode 5 length */
2026 saa_write8(plines,0); /* Std opcode 6 length */
2027 saa_write8(plines,0); /* Std opcode 7 length */
2028 saa_write8(plines,0); /* Std opcode 8 length */
2029 saa_write8(plines,1); /* Std opcode 9 length */
2030 saa_write8(plines,0); /* Std opcode 10 length */
2031 saa_write8(plines,0); /* Std opcode 11 length */
2032 saa_write8(plines,1); /* Std opcode 12 length */
2033 /* Directory Table */
2034 saa_write8(plines,0); /* End of table */
2035 /* File Name Table */
2036 ftentry = dwarf_flist;
2037 for (indx = 0;indx<dwarf_numfiles;indx++)
2039 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2040 saa_write8(plines,0); /* directory LEB128u */
2041 saa_write8(plines,0); /* time LEB128u */
2042 saa_write8(plines,0); /* size LEB128u */
2043 ftentry = ftentry->next;
2045 saa_write8(plines,0); /* End of table */
2046 linepoff = plines->datalen;
2047 linelen = linepoff + totlen + 10;
2048 linebuf = pbuf = nasm_malloc(linelen);
2049 WRITELONG(pbuf,linelen-4); /* initial length */
2050 WRITESHORT(pbuf,3); /* dwarf version */
2051 WRITELONG(pbuf,linepoff); /* offset to line number program */
2052 /* write line header */
2053 saalen = linepoff;
2054 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2055 pbuf += linepoff;
2056 saa_free(plines);
2057 /* concatonate line program ranges */
2058 linepoff += 13;
2059 plinesrel = saa_init(1L);
2060 psect = dwarf_fsect;
2061 for (indx = 0; indx < dwarf_nsections; indx++)
2063 saa_write32(plinesrel, linepoff);
2064 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
2065 saa_write32(plinesrel, (uint32_t) 0);
2066 plinep = psect->psaa;
2067 saalen = plinep->datalen;
2068 saa_rnbytes(plinep, pbuf, saalen);
2069 pbuf += saalen;
2070 linepoff += saalen;
2071 saa_free(plinep);
2072 /* done with this entry */
2073 psect = psect->next;
2077 /* build rela.lines section */
2078 linerellen =saalen = plinesrel->datalen;
2079 linerelbuf = pbuf = nasm_malloc(linerellen);
2080 saa_rnbytes(plinesrel, pbuf, saalen);
2081 saa_free(plinesrel);
2083 /* build frame section */
2084 framelen = 4;
2085 framebuf = pbuf = nasm_malloc(framelen);
2086 WRITELONG(pbuf,framelen-4); /* initial length */
2088 /* build loc section */
2089 loclen = 16;
2090 locbuf = pbuf = nasm_malloc(loclen);
2091 WRITELONG(pbuf,0); /* null beginning offset */
2092 WRITELONG(pbuf,0); /* null ending offset */
2095 static void dwarf32_cleanup(void)
2097 if (arangesbuf)
2098 nasm_free(arangesbuf);
2099 if (arangesrelbuf)
2100 nasm_free(arangesrelbuf);
2101 if (pubnamesbuf)
2102 nasm_free(pubnamesbuf);
2103 if (infobuf)
2104 nasm_free(infobuf);
2105 if (inforelbuf)
2106 nasm_free(inforelbuf);
2107 if (abbrevbuf)
2108 nasm_free(abbrevbuf);
2109 if (linebuf)
2110 nasm_free(linebuf);
2111 if (linerelbuf)
2112 nasm_free(linerelbuf);
2113 if (framebuf)
2114 nasm_free(framebuf);
2115 if (locbuf)
2116 nasm_free(locbuf);
2118 static void dwarf32_findfile(const char * fname)
2120 int finx;
2121 struct linelist *match;
2123 /* return if fname is current file name */
2124 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2125 /* search for match */
2126 else
2128 match = 0;
2129 if (dwarf_flist)
2131 match = dwarf_flist;
2132 for (finx = 0; finx < dwarf_numfiles; finx++)
2134 if (!(strcmp(fname, match->filename)))
2136 dwarf_clist = match;
2137 return;
2141 /* add file name to end of list */
2142 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2143 dwarf_numfiles++;
2144 dwarf_clist->line = dwarf_numfiles;
2145 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2146 strcpy(dwarf_clist->filename,fname);
2147 dwarf_clist->next = 0;
2148 /* if first entry */
2149 if (!dwarf_flist)
2151 dwarf_flist = dwarf_elist = dwarf_clist;
2152 dwarf_clist->last = 0;
2154 /* chain to previous entry */
2155 else
2157 dwarf_elist->next = dwarf_clist;
2158 dwarf_elist = dwarf_clist;
2162 /* */
2163 static void dwarf32_findsect(const int index)
2165 int sinx;
2166 struct sectlist *match;
2167 struct SAA *plinep;
2168 /* return if index is current section index */
2169 if (dwarf_csect && (dwarf_csect->section == index))
2171 return;
2173 /* search for match */
2174 else
2176 match = 0;
2177 if (dwarf_fsect)
2179 match = dwarf_fsect;
2180 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2182 if ((match->section == index))
2184 dwarf_csect = match;
2185 return;
2187 match = match->next;
2190 /* add entry to end of list */
2191 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2192 dwarf_nsections++;
2193 dwarf_csect->psaa = plinep = saa_init(1L);
2194 dwarf_csect->line = 1;
2195 dwarf_csect->offset = 0;
2196 dwarf_csect->file = 1;
2197 dwarf_csect->section = index;
2198 dwarf_csect->next = 0;
2199 /* set relocatable address at start of line program */
2200 saa_write8(plinep,DW_LNS_extended_op);
2201 saa_write8(plinep,5); /* operand length */
2202 saa_write8(plinep,DW_LNE_set_address);
2203 saa_write32(plinep,0); /* Start Address */
2204 /* if first entry */
2205 if (!dwarf_fsect)
2207 dwarf_fsect = dwarf_esect = dwarf_csect;
2208 dwarf_csect->last = 0;
2210 /* chain to previous entry */
2211 else
2213 dwarf_esect->next = dwarf_csect;
2214 dwarf_esect = dwarf_csect;
2219 #endif /* OF_ELF */