ELF: clean up the arithmetic for global symbols
[nasm.git] / output / outelf32.c
blob832a503479d91ccd9d54b5696a677e9dd058bc9a
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 "outform.h"
24 #include "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 void debug32_typevalue(int32_t);
185 void debug32_deflabel(char *, int32_t, int64_t, int, char *);
186 void debug32_directive(const char *, const char *);
188 /* stabs debugging routines */
189 void stabs32_init(struct ofmt *, void *, FILE *, efunc);
190 void stabs32_linenum(const char *filename, int32_t linenumber, int32_t);
191 void stabs32_output(int, void *);
192 void stabs32_generate(void);
193 void stabs32_cleanup(void);
195 /* dwarf debugging routines */
196 void dwarf32_init(struct ofmt *, void *, FILE *, efunc);
197 void dwarf32_linenum(const char *filename, int32_t linenumber, int32_t);
198 void dwarf32_output(int, void *);
199 void dwarf32_generate(void);
200 void dwarf32_cleanup(void);
201 void dwarf32_findfile(const char *);
202 void dwarf32_findsect(const int);
203 void saa_wleb128u(struct SAA *, int);
204 void saa_wleb128s(struct SAA *, int);
207 * Special NASM section numbers which are used to define ELF special
208 * symbols, which can be used with WRT to provide PIC and TLS
209 * relocation types.
211 static int32_t elf_gotpc_sect, elf_gotoff_sect;
212 static int32_t elf_got_sect, elf_plt_sect;
213 static int32_t elf_sym_sect, elf_tlsie_sect;
215 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
217 elffp = fp;
218 error = errfunc;
219 evaluate = eval;
220 (void)ldef; /* placate optimisers */
221 sects = NULL;
222 nsects = sectlen = 0;
223 syms = saa_init((int32_t)sizeof(struct Symbol));
224 nlocals = nglobs = ndebugs = 0;
225 bsym = raa_init();
226 strs = saa_init(1L);
227 saa_wbytes(strs, "\0", 1L);
228 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
229 strslen = 2 + strlen(elf_module);
230 shstrtab = NULL;
231 shstrtablen = shstrtabsize = 0;;
232 add_sectname("", "");
234 fwds = NULL;
236 elf_gotpc_sect = seg_alloc();
237 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf32,
238 error);
239 elf_gotoff_sect = seg_alloc();
240 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf32,
241 error);
242 elf_got_sect = seg_alloc();
243 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf32,
244 error);
245 elf_plt_sect = seg_alloc();
246 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf32,
247 error);
248 elf_sym_sect = seg_alloc();
249 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf32,
250 error);
251 elf_tlsie_sect = seg_alloc();
252 ldef("..tlsie", elf_tlsie_sect + 1, 0L, NULL, false, false, &of_elf32,
253 error);
255 def_seg = seg_alloc();
258 static void elf_init_hack(FILE * fp, efunc errfunc, ldfunc ldef,
259 evalfunc eval)
261 of_elf32.current_dfmt = of_elf.current_dfmt; /* Sync debugging format */
262 elf_init(fp, errfunc, ldef, eval);
265 static void elf_cleanup(int debuginfo)
267 struct Reloc *r;
268 int i;
270 (void)debuginfo;
272 elf_write();
273 fclose(elffp);
274 for (i = 0; i < nsects; i++) {
275 if (sects[i]->type != SHT_NOBITS)
276 saa_free(sects[i]->data);
277 if (sects[i]->head)
278 saa_free(sects[i]->rel);
279 while (sects[i]->head) {
280 r = sects[i]->head;
281 sects[i]->head = sects[i]->head->next;
282 nasm_free(r);
285 nasm_free(sects);
286 saa_free(syms);
287 raa_free(bsym);
288 saa_free(strs);
289 if (of_elf32.current_dfmt) {
290 of_elf32.current_dfmt->cleanup();
294 static void add_sectname(char *firsthalf, char *secondhalf)
296 int len = strlen(firsthalf) + strlen(secondhalf);
297 while (shstrtablen + len + 1 > shstrtabsize)
298 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
299 strcpy(shstrtab + shstrtablen, firsthalf);
300 strcat(shstrtab + shstrtablen, secondhalf);
301 shstrtablen += len + 1;
304 static int elf_make_section(char *name, int type, int flags, int align)
306 struct Section *s;
308 s = nasm_malloc(sizeof(*s));
310 if (type != SHT_NOBITS)
311 s->data = saa_init(1L);
312 s->head = NULL;
313 s->tail = &s->head;
314 s->len = s->size = 0;
315 s->nrelocs = 0;
316 if (!strcmp(name, ".text"))
317 s->index = def_seg;
318 else
319 s->index = seg_alloc();
320 add_sectname("", name);
321 s->name = nasm_malloc(1 + strlen(name));
322 strcpy(s->name, name);
323 s->type = type;
324 s->flags = flags;
325 s->align = align;
326 s->gsyms = NULL;
328 if (nsects >= sectlen)
329 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
330 sects[nsects++] = s;
332 return nsects - 1;
336 static int32_t elf_section_names(char *name, int pass, int *bits)
338 char *p;
339 uint32_t flags, flags_and, flags_or;
340 uint32_t align;
341 int type, i;
344 * Default is 32 bits.
346 if (!name) {
347 *bits = 32;
348 return def_seg;
351 p = name;
352 while (*p && !nasm_isspace(*p))
353 p++;
354 if (*p)
355 *p++ = '\0';
356 flags_and = flags_or = type = align = 0;
358 while (*p && nasm_isspace(*p))
359 p++;
360 while (*p) {
361 char *q = p;
362 while (*p && !nasm_isspace(*p))
363 p++;
364 if (*p)
365 *p++ = '\0';
366 while (*p && nasm_isspace(*p))
367 p++;
369 if (!nasm_strnicmp(q, "align=", 6)) {
370 align = atoi(q + 6);
371 if (align == 0)
372 align = 1;
373 if ((align - 1) & align) { /* means it's not a power of two */
374 error(ERR_NONFATAL, "section alignment %d is not"
375 " a power of two", align);
376 align = 1;
378 } else if (!nasm_stricmp(q, "alloc")) {
379 flags_and |= SHF_ALLOC;
380 flags_or |= SHF_ALLOC;
381 } else if (!nasm_stricmp(q, "noalloc")) {
382 flags_and |= SHF_ALLOC;
383 flags_or &= ~SHF_ALLOC;
384 } else if (!nasm_stricmp(q, "exec")) {
385 flags_and |= SHF_EXECINSTR;
386 flags_or |= SHF_EXECINSTR;
387 } else if (!nasm_stricmp(q, "noexec")) {
388 flags_and |= SHF_EXECINSTR;
389 flags_or &= ~SHF_EXECINSTR;
390 } else if (!nasm_stricmp(q, "write")) {
391 flags_and |= SHF_WRITE;
392 flags_or |= SHF_WRITE;
393 } else if (!nasm_stricmp(q, "tls")) {
394 flags_and |= SHF_TLS;
395 flags_or |= SHF_TLS;
396 } else if (!nasm_stricmp(q, "nowrite")) {
397 flags_and |= SHF_WRITE;
398 flags_or &= ~SHF_WRITE;
399 } else if (!nasm_stricmp(q, "progbits")) {
400 type = SHT_PROGBITS;
401 } else if (!nasm_stricmp(q, "nobits")) {
402 type = SHT_NOBITS;
403 } else if (pass == 1) error(ERR_WARNING, "Unknown section attribute '%s' ignored on"
404 " declaration of section `%s'", q, name);
407 if (!strcmp(name, ".shstrtab") ||
408 !strcmp(name, ".symtab") ||
409 !strcmp(name, ".strtab")) {
410 error(ERR_NONFATAL, "attempt to redefine reserved section"
411 "name `%s'", name);
412 return NO_SEG;
415 for (i = 0; i < nsects; i++)
416 if (!strcmp(name, sects[i]->name))
417 break;
418 if (i == nsects) {
419 const struct elf_known_section *ks = elf_known_sections;
421 while (ks->name) {
422 if (!strcmp(name, ks->name))
423 break;
424 ks++;
427 type = type ? type : ks->type;
428 align = align ? align : ks->align;
429 flags = (ks->flags & ~flags_and) | flags_or;
431 i = elf_make_section(name, type, flags, align);
432 } else if (pass == 1) {
433 if ((type && sects[i]->type != type)
434 || (align && sects[i]->align != align)
435 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
436 error(ERR_WARNING, "section attributes ignored on"
437 " redeclaration of section `%s'", name);
440 return sects[i]->index;
443 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
444 int is_global, char *special)
446 int pos = strslen;
447 struct Symbol *sym;
448 bool special_used = false;
450 #if defined(DEBUG) && DEBUG>2
451 fprintf(stderr,
452 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
453 name, segment, offset, is_global, special);
454 #endif
455 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
457 * This is a NASM special symbol. We never allow it into
458 * the ELF symbol table, even if it's a valid one. If it
459 * _isn't_ a valid one, we should barf immediately.
461 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
462 strcmp(name, "..got") && strcmp(name, "..plt") &&
463 strcmp(name, "..sym") && strcmp(name, "..tlsie"))
464 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
465 return;
468 if (is_global == 3) {
469 struct Symbol **s;
471 * Fix up a forward-reference symbol size from the first
472 * pass.
474 for (s = &fwds; *s; s = &(*s)->nextfwd)
475 if (!strcmp((*s)->name, name)) {
476 struct tokenval tokval;
477 expr *e;
478 char *p = special;
480 while (*p && !nasm_isspace(*p))
481 p++;
482 while (*p && nasm_isspace(*p))
483 p++;
484 stdscan_reset();
485 stdscan_bufptr = p;
486 tokval.t_type = TOKEN_INVALID;
487 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
488 if (e) {
489 if (!is_simple(e))
490 error(ERR_NONFATAL, "cannot use relocatable"
491 " expression as symbol size");
492 else
493 (*s)->size = reloc_value(e);
497 * Remove it from the list of unresolved sizes.
499 nasm_free((*s)->name);
500 *s = (*s)->nextfwd;
501 return;
503 return; /* it wasn't an important one */
506 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
507 strslen += 1 + strlen(name);
509 lastsym = sym = saa_wstruct(syms);
511 memset(&sym->symv, 0, sizeof(struct rbtree));
513 sym->strpos = pos;
514 sym->type = is_global ? SYM_GLOBAL : 0;
515 sym->other = STV_DEFAULT;
516 sym->size = 0;
517 if (segment == NO_SEG)
518 sym->section = SHN_ABS;
519 else {
520 int i;
521 sym->section = SHN_UNDEF;
522 if (nsects == 0 && segment == def_seg) {
523 int tempint;
524 if (segment != elf_section_names(".text", 2, &tempint))
525 error(ERR_PANIC,
526 "strange segment conditions in ELF driver");
527 sym->section = nsects;
528 } else {
529 for (i = 0; i < nsects; i++)
530 if (segment == sects[i]->index) {
531 sym->section = i + 1;
532 break;
537 if (is_global == 2) {
538 sym->size = offset;
539 sym->symv.key = 0;
540 sym->section = SHN_COMMON;
542 * We have a common variable. Check the special text to see
543 * if it's a valid number and power of two; if so, store it
544 * as the alignment for the common variable.
546 if (special) {
547 bool err;
548 sym->symv.key = readnum(special, &err);
549 if (err)
550 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
551 " valid number", special);
552 else if ((sym->symv.key | (sym->symv.key - 1))
553 != 2 * sym->symv.key - 1)
554 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
555 " power of two", special);
557 special_used = true;
558 } else
559 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
561 if (sym->type == SYM_GLOBAL) {
563 * If sym->section == SHN_ABS, then the first line of the
564 * else section would cause a core dump, because its a reference
565 * beyond the end of the section array.
566 * This behaviour is exhibited by this code:
567 * GLOBAL crash_nasm
568 * crash_nasm equ 0
569 * To avoid such a crash, such requests are silently discarded.
570 * This may not be the best solution.
572 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
573 bsym = raa_write(bsym, segment, nglobs);
574 } else if (sym->section != SHN_ABS) {
576 * This is a global symbol; so we must add it to the rbtree
577 * of global symbols in its section.
579 * In addition, we check the special text for symbol
580 * type and size information.
582 sects[sym->section-1]->gsyms =
583 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
585 if (special) {
586 int n = strcspn(special, " \t");
588 if (!nasm_strnicmp(special, "function", n))
589 sym->type |= STT_FUNC;
590 else if (!nasm_strnicmp(special, "data", n) ||
591 !nasm_strnicmp(special, "object", n))
592 sym->type |= STT_OBJECT;
593 else if (!nasm_strnicmp(special, "notype", n))
594 sym->type |= STT_NOTYPE;
595 else
596 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
597 n, special);
598 special += n;
600 while (nasm_isspace(*special))
601 ++special;
602 if (*special) {
603 n = strcspn(special, " \t");
604 if (!nasm_strnicmp(special, "default", n))
605 sym->other = STV_DEFAULT;
606 else if (!nasm_strnicmp(special, "internal", n))
607 sym->other = STV_INTERNAL;
608 else if (!nasm_strnicmp(special, "hidden", n))
609 sym->other = STV_HIDDEN;
610 else if (!nasm_strnicmp(special, "protected", n))
611 sym->other = STV_PROTECTED;
612 else
613 n = 0;
614 special += n;
617 if (*special) {
618 struct tokenval tokval;
619 expr *e;
620 int fwd = 0;
621 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
623 while (special[n] && nasm_isspace(special[n]))
624 n++;
626 * We have a size expression; attempt to
627 * evaluate it.
629 stdscan_reset();
630 stdscan_bufptr = special + n;
631 tokval.t_type = TOKEN_INVALID;
632 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
633 NULL);
634 if (fwd) {
635 sym->nextfwd = fwds;
636 fwds = sym;
637 sym->name = nasm_strdup(name);
638 } else if (e) {
639 if (!is_simple(e))
640 error(ERR_NONFATAL, "cannot use relocatable"
641 " expression as symbol size");
642 else
643 sym->size = reloc_value(e);
645 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
647 special_used = true;
650 * If TLS segment, mark symbol accordingly.
652 if (sects[sym->section - 1]->flags & SHF_TLS) {
653 sym->type &= 0xf0;
654 sym->type |= STT_TLS;
657 sym->globnum = nglobs;
658 nglobs++;
659 } else
660 nlocals++;
662 if (special && !special_used)
663 error(ERR_NONFATAL, "no special symbol features supported here");
666 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
668 struct Reloc *r;
670 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
671 sect->tail = &r->next;
672 r->next = NULL;
674 r->address = sect->len;
675 if (segment == NO_SEG)
676 r->symbol = 0;
677 else {
678 int i;
679 r->symbol = 0;
680 for (i = 0; i < nsects; i++)
681 if (segment == sects[i]->index)
682 r->symbol = i + 2;
683 if (!r->symbol)
684 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
686 r->type = type;
688 sect->nrelocs++;
692 * This routine deals with ..got and ..sym relocations: the more
693 * complicated kinds. In shared-library writing, some relocations
694 * with respect to global symbols must refer to the precise symbol
695 * rather than referring to an offset from the base of the section
696 * _containing_ the symbol. Such relocations call to this routine,
697 * which searches the symbol list for the symbol in question.
699 * R_386_GOT32 references require the _exact_ symbol address to be
700 * used; R_386_32 references can be at an offset from the symbol.
701 * The boolean argument `exact' tells us this.
703 * Return value is the adjusted value of `addr', having become an
704 * offset from the symbol rather than the section. Should always be
705 * zero when returning from an exact call.
707 * Limitation: if you define two symbols at the same place,
708 * confusion will occur.
710 * Inefficiency: we search, currently, using a linked list which
711 * isn't even necessarily sorted.
713 static int32_t elf_add_gsym_reloc(struct Section *sect,
714 int32_t segment, uint32_t offset,
715 int type, bool exact)
717 struct Reloc *r;
718 struct Section *s;
719 struct Symbol *sym;
720 struct rbtree *srb;
721 int i;
724 * First look up the segment/offset pair and find a global
725 * symbol corresponding to it. If it's not one of our segments,
726 * then it must be an external symbol, in which case we're fine
727 * doing a normal elf_add_reloc after first sanity-checking
728 * that the offset from the symbol is zero.
730 s = NULL;
731 for (i = 0; i < nsects; i++)
732 if (segment == sects[i]->index) {
733 s = sects[i];
734 break;
736 if (!s) {
737 if (exact && offset != 0)
738 error(ERR_NONFATAL, "unable to find a suitable global symbol"
739 " for this reference");
740 else
741 elf_add_reloc(sect, segment, type);
742 return offset;
745 srb = rb_search(s->gsyms, offset);
746 if (!srb || (exact && srb->key != offset)) {
747 error(ERR_NONFATAL, "unable to find a suitable global symbol"
748 " for this reference");
749 return 0;
751 sym = container_of(srb, struct Symbol, symv);
753 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
754 sect->tail = &r->next;
755 r->next = NULL;
757 r->address = sect->len;
758 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
759 r->type = type;
761 sect->nrelocs++;
763 return offset - sym->symv.key;
766 static void elf_out(int32_t segto, const void *data,
767 enum out_type type, uint64_t size,
768 int32_t segment, int32_t wrt)
770 struct Section *s;
771 int32_t addr;
772 uint8_t mydata[4], *p;
773 int i;
774 static struct symlininfo sinfo;
777 * handle absolute-assembly (structure definitions)
779 if (segto == NO_SEG) {
780 if (type != OUT_RESERVE)
781 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
782 " space");
783 return;
786 s = NULL;
787 for (i = 0; i < nsects; i++)
788 if (segto == sects[i]->index) {
789 s = sects[i];
790 break;
792 if (!s) {
793 int tempint; /* ignored */
794 if (segto != elf_section_names(".text", 2, &tempint))
795 error(ERR_PANIC, "strange segment conditions in ELF driver");
796 else {
797 s = sects[nsects - 1];
798 i = nsects - 1;
802 /* again some stabs debugging stuff */
803 if (of_elf32.current_dfmt) {
804 sinfo.offset = s->len;
805 sinfo.section = i;
806 sinfo.name = s->name;
807 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
809 /* end of debugging stuff */
811 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
812 error(ERR_WARNING, "attempt to initialize memory in"
813 " BSS section `%s': ignored", s->name);
814 s->len += realsize(type, size);
815 return;
818 if (type == OUT_RESERVE) {
819 if (s->type == SHT_PROGBITS) {
820 error(ERR_WARNING, "uninitialized space declared in"
821 " non-BSS section `%s': zeroing", s->name);
822 elf_sect_write(s, NULL, size);
823 } else
824 s->len += size;
825 } else if (type == OUT_RAWDATA) {
826 if (segment != NO_SEG)
827 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
828 elf_sect_write(s, data, size);
829 } else if (type == OUT_ADDRESS) {
830 bool gnu16 = false;
831 addr = *(int64_t *)data;
832 if (segment != NO_SEG) {
833 if (segment % 2) {
834 error(ERR_NONFATAL, "ELF format does not support"
835 " segment base references");
836 } else {
837 if (wrt == NO_SEG) {
838 if (size == 2) {
839 gnu16 = true;
840 elf_add_reloc(s, segment, R_386_16);
841 } else {
842 elf_add_reloc(s, segment, R_386_32);
844 } else if (wrt == elf_gotpc_sect + 1) {
846 * The user will supply GOT relative to $$. ELF
847 * will let us have GOT relative to $. So we
848 * need to fix up the data item by $-$$.
850 addr += s->len;
851 elf_add_reloc(s, segment, R_386_GOTPC);
852 } else if (wrt == elf_gotoff_sect + 1) {
853 elf_add_reloc(s, segment, R_386_GOTOFF);
854 } else if (wrt == elf_tlsie_sect + 1) {
855 addr = elf_add_gsym_reloc(s, segment, addr,
856 R_386_TLS_IE, true);
857 } else if (wrt == elf_got_sect + 1) {
858 addr = elf_add_gsym_reloc(s, segment, addr,
859 R_386_GOT32, true);
860 } else if (wrt == elf_sym_sect + 1) {
861 if (size == 2) {
862 gnu16 = true;
863 addr = elf_add_gsym_reloc(s, segment, addr,
864 R_386_16, false);
865 } else {
866 addr = elf_add_gsym_reloc(s, segment, addr,
867 R_386_32, false);
869 } else if (wrt == elf_plt_sect + 1) {
870 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
871 "relative PLT references");
872 } else {
873 error(ERR_NONFATAL, "ELF format does not support this"
874 " use of WRT");
875 wrt = NO_SEG; /* we can at least _try_ to continue */
879 p = mydata;
880 if (gnu16) {
881 error(ERR_WARNING | ERR_WARN_GNUELF,
882 "16-bit relocations in ELF is a GNU extension");
883 WRITESHORT(p, addr);
884 } else {
885 if (size != 4 && segment != NO_SEG) {
886 error(ERR_NONFATAL,
887 "Unsupported non-32-bit ELF relocation");
889 WRITELONG(p, addr);
891 elf_sect_write(s, mydata, size);
892 } else if (type == OUT_REL2ADR) {
893 if (segment == segto)
894 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
895 if (segment != NO_SEG && segment % 2) {
896 error(ERR_NONFATAL, "ELF format does not support"
897 " segment base references");
898 } else {
899 if (wrt == NO_SEG) {
900 error(ERR_WARNING | ERR_WARN_GNUELF,
901 "16-bit relocations in ELF is a GNU extension");
902 elf_add_reloc(s, segment, R_386_PC16);
903 } else {
904 error(ERR_NONFATAL,
905 "Unsupported non-32-bit ELF relocation");
908 p = mydata;
909 WRITESHORT(p, *(int64_t *)data - size);
910 elf_sect_write(s, mydata, 2L);
911 } else if (type == OUT_REL4ADR) {
912 if (segment == segto)
913 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
914 if (segment != NO_SEG && segment % 2) {
915 error(ERR_NONFATAL, "ELF format does not support"
916 " segment base references");
917 } else {
918 if (wrt == NO_SEG) {
919 elf_add_reloc(s, segment, R_386_PC32);
920 } else if (wrt == elf_plt_sect + 1) {
921 elf_add_reloc(s, segment, R_386_PLT32);
922 } else if (wrt == elf_gotpc_sect + 1 ||
923 wrt == elf_gotoff_sect + 1 ||
924 wrt == elf_got_sect + 1) {
925 error(ERR_NONFATAL, "ELF format cannot produce PC-"
926 "relative GOT references");
927 } else {
928 error(ERR_NONFATAL, "ELF format does not support this"
929 " use of WRT");
930 wrt = NO_SEG; /* we can at least _try_ to continue */
933 p = mydata;
934 WRITELONG(p, *(int64_t *)data - size);
935 elf_sect_write(s, mydata, 4L);
939 static void elf_write(void)
941 int align;
942 char *p;
943 int i;
945 struct SAA *symtab;
946 int32_t symtablen, symtablocal;
949 * Work out how many sections we will have. We have SHN_UNDEF,
950 * then the flexible user sections, then the fixed sections
951 * `.shstrtab', `.symtab' and `.strtab', then optionally
952 * relocation sections for the user sections.
954 nsections = sec_numspecial + 1;
955 if (of_elf32.current_dfmt == &df_stabs)
956 nsections += 3;
957 else if (of_elf32.current_dfmt == &df_dwarf)
958 nsections += 10;
960 add_sectname("", ".shstrtab");
961 add_sectname("", ".symtab");
962 add_sectname("", ".strtab");
963 for (i = 0; i < nsects; i++) {
964 nsections++; /* for the section itself */
965 if (sects[i]->head) {
966 nsections++; /* for its relocations */
967 add_sectname(".rel", sects[i]->name);
971 if (of_elf32.current_dfmt == &df_stabs) {
972 /* in case the debug information is wanted, just add these three sections... */
973 add_sectname("", ".stab");
974 add_sectname("", ".stabstr");
975 add_sectname(".rel", ".stab");
976 } else if (of_elf32.current_dfmt == &df_dwarf) {
977 /* the dwarf debug standard specifies the following ten sections,
978 not all of which are currently implemented,
979 although all of them are defined. */
980 add_sectname("", ".debug_aranges");
981 add_sectname(".rela", ".debug_aranges");
982 add_sectname("", ".debug_pubnames");
983 add_sectname("", ".debug_info");
984 add_sectname(".rela", ".debug_info");
985 add_sectname("", ".debug_abbrev");
986 add_sectname("", ".debug_line");
987 add_sectname(".rela", ".debug_line");
988 add_sectname("", ".debug_frame");
989 add_sectname("", ".debug_loc");
993 * Output the ELF header.
995 fwrite("\177ELF\1\1\1", 7, 1, elffp);
996 fputc(elf_osabi, elffp);
997 fputc(elf_abiver, elffp);
998 fwritezero(7, elffp);
999 fwriteint16_t(1, elffp); /* ET_REL relocatable file */
1000 fwriteint16_t(3, elffp); /* EM_386 processor ID */
1001 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1002 fwriteint32_t(0L, elffp); /* no entry point */
1003 fwriteint32_t(0L, elffp); /* no program header table */
1004 fwriteint32_t(0x40L, elffp); /* section headers straight after
1005 * ELF header plus alignment */
1006 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1007 fwriteint16_t(0x34, elffp); /* size of ELF header */
1008 fwriteint16_t(0, elffp); /* no program header table, again */
1009 fwriteint16_t(0, elffp); /* still no program header table */
1010 fwriteint16_t(0x28, elffp); /* size of section header */
1011 fwriteint16_t(nsections, elffp); /* number of sections */
1012 fwriteint16_t(sec_shstrtab, elffp); /* string table section index for
1013 * section header table */
1014 fwriteint32_t(0L, elffp); /* align to 0x40 bytes */
1015 fwriteint32_t(0L, elffp);
1016 fwriteint32_t(0L, elffp);
1019 * Build the symbol table and relocation tables.
1021 symtab = elf_build_symtab(&symtablen, &symtablocal);
1022 for (i = 0; i < nsects; i++)
1023 if (sects[i]->head)
1024 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1025 sects[i]->head);
1028 * Now output the section header table.
1031 elf_foffs = 0x40 + 0x28 * nsections;
1032 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1033 elf_foffs += align;
1034 elf_nsect = 0;
1035 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1037 /* SHN_UNDEF */
1038 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1039 p = shstrtab + 1;
1041 /* The normal sections */
1042 for (i = 0; i < nsects; i++) {
1043 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1044 (sects[i]->type == SHT_PROGBITS ?
1045 sects[i]->data : NULL), true,
1046 sects[i]->len, 0, 0, sects[i]->align, 0);
1047 p += strlen(p) + 1;
1050 /* .shstrtab */
1051 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1052 shstrtablen, 0, 0, 1, 0);
1053 p += strlen(p) + 1;
1055 /* .symtab */
1056 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1057 symtablen, sec_strtab, symtablocal, 4, 16);
1058 p += strlen(p) + 1;
1060 /* .strtab */
1061 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1062 strslen, 0, 0, 1, 0);
1063 p += strlen(p) + 1;
1065 /* The relocation sections */
1066 for (i = 0; i < nsects; i++)
1067 if (sects[i]->head) {
1068 elf_section_header(p - shstrtab, SHT_REL, 0, sects[i]->rel, true,
1069 sects[i]->rellen, sec_symtab, i + 1, 4, 8);
1070 p += strlen(p) + 1;
1074 if (of_elf32.current_dfmt == &df_stabs) {
1075 /* for debugging information, create the last three sections
1076 which are the .stab , .stabstr and .rel.stab sections respectively */
1078 /* this function call creates the stab sections in memory */
1079 stabs32_generate();
1081 if (stabbuf && stabstrbuf && stabrelbuf) {
1082 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1083 stablen, sec_stabstr, 0, 4, 12);
1084 p += strlen(p) + 1;
1086 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1087 stabstrlen, 0, 0, 4, 0);
1088 p += strlen(p) + 1;
1090 /* link -> symtable info -> section to refer to */
1091 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1092 stabrellen, sec_symtab, sec_stab, 4, 8);
1093 p += strlen(p) + 1;
1095 } else if (of_elf32.current_dfmt == &df_dwarf) {
1096 /* for dwarf debugging information, create the ten dwarf sections */
1098 /* this function call creates the dwarf sections in memory */
1099 if (dwarf_fsect)
1100 dwarf32_generate();
1102 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1103 arangeslen, 0, 0, 1, 0);
1104 p += strlen(p) + 1;
1106 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1107 arangesrellen, sec_symtab, sec_debug_aranges,
1108 1, 12);
1109 p += strlen(p) + 1;
1111 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf,
1112 false, pubnameslen, 0, 0, 1, 0);
1113 p += strlen(p) + 1;
1115 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1116 infolen, 0, 0, 1, 0);
1117 p += strlen(p) + 1;
1119 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1120 inforellen, sec_symtab, sec_debug_info, 1, 12);
1121 p += strlen(p) + 1;
1123 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1124 abbrevlen, 0, 0, 1, 0);
1125 p += strlen(p) + 1;
1127 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1128 linelen, 0, 0, 1, 0);
1129 p += strlen(p) + 1;
1131 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1132 linerellen, sec_symtab, sec_debug_line, 1, 12);
1133 p += strlen(p) + 1;
1135 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1136 framelen, 0, 0, 8, 0);
1137 p += strlen(p) + 1;
1139 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1140 loclen, 0, 0, 1, 0);
1141 p += strlen(p) + 1;
1143 fwritezero(align, elffp);
1146 * Now output the sections.
1148 elf_write_sections();
1150 nasm_free(elf_sects);
1151 saa_free(symtab);
1154 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1156 struct SAA *s = saa_init(1L);
1157 struct Symbol *sym;
1158 uint8_t entry[16], *p;
1159 int i;
1161 *len = *local = 0;
1164 * First, an all-zeros entry, required by the ELF spec.
1166 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1167 *len += 16;
1168 (*local)++;
1171 * Next, an entry for the file name.
1173 p = entry;
1174 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1175 WRITELONG(p, 0); /* no value */
1176 WRITELONG(p, 0); /* no size either */
1177 WRITESHORT(p, STT_FILE); /* type FILE */
1178 WRITESHORT(p, SHN_ABS);
1179 saa_wbytes(s, entry, 16L);
1180 *len += 16;
1181 (*local)++;
1184 * Now some standard symbols defining the segments, for relocation
1185 * purposes.
1187 for (i = 1; i <= nsects; i++) {
1188 p = entry;
1189 WRITELONG(p, 0); /* no symbol name */
1190 WRITELONG(p, 0); /* offset zero */
1191 WRITELONG(p, 0); /* size zero */
1192 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1193 WRITESHORT(p, i); /* section id */
1194 saa_wbytes(s, entry, 16L);
1195 *len += 16;
1196 (*local)++;
1200 * Now the other local symbols.
1202 saa_rewind(syms);
1203 while ((sym = saa_rstruct(syms))) {
1204 if (sym->type & SYM_GLOBAL)
1205 continue;
1206 p = entry;
1207 WRITELONG(p, sym->strpos);
1208 WRITELONG(p, sym->symv.key);
1209 WRITELONG(p, sym->size);
1210 WRITECHAR(p, sym->type); /* type and binding */
1211 WRITECHAR(p, sym->other); /* visibility */
1212 WRITESHORT(p, sym->section);
1213 saa_wbytes(s, entry, 16L);
1214 *len += 16;
1215 (*local)++;
1218 * dwarf needs symbols for debug sections
1219 * which are relocation targets.
1221 //*** fix for 32 bit
1222 if (of_elf32.current_dfmt == &df_dwarf) {
1223 dwarf_infosym = *local;
1224 p = entry;
1225 WRITELONG(p, 0); /* no symbol name */
1226 WRITELONG(p, (uint32_t) 0); /* offset zero */
1227 WRITELONG(p, (uint32_t) 0); /* size zero */
1228 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1229 WRITESHORT(p, sec_debug_info); /* section id */
1230 saa_wbytes(s, entry, 16L);
1231 *len += 16;
1232 (*local)++;
1233 dwarf_abbrevsym = *local;
1234 p = entry;
1235 WRITELONG(p, 0); /* no symbol name */
1236 WRITELONG(p, (uint32_t) 0); /* offset zero */
1237 WRITELONG(p, (uint32_t) 0); /* size zero */
1238 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1239 WRITESHORT(p, sec_debug_abbrev); /* section id */
1240 saa_wbytes(s, entry, 16L);
1241 *len += 16;
1242 (*local)++;
1243 dwarf_linesym = *local;
1244 p = entry;
1245 WRITELONG(p, 0); /* no symbol name */
1246 WRITELONG(p, (uint32_t) 0); /* offset zero */
1247 WRITELONG(p, (uint32_t) 0); /* size zero */
1248 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1249 WRITESHORT(p, sec_debug_line); /* section id */
1250 saa_wbytes(s, entry, 16L);
1251 *len += 16;
1252 (*local)++;
1256 * Now the global symbols.
1258 saa_rewind(syms);
1259 while ((sym = saa_rstruct(syms))) {
1260 if (!(sym->type & SYM_GLOBAL))
1261 continue;
1262 p = entry;
1263 WRITELONG(p, sym->strpos);
1264 WRITELONG(p, sym->symv.key);
1265 WRITELONG(p, sym->size);
1266 WRITECHAR(p, sym->type); /* type and binding */
1267 WRITECHAR(p, sym->other); /* visibility */
1268 WRITESHORT(p, sym->section);
1269 saa_wbytes(s, entry, 16L);
1270 *len += 16;
1273 return s;
1276 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1278 struct SAA *s;
1279 uint8_t *p, entry[8];
1281 if (!r)
1282 return NULL;
1284 s = saa_init(1L);
1285 *len = 0;
1287 while (r) {
1288 int32_t sym = r->symbol;
1291 * Create a real symbol index; the +2 refers to the two special
1292 * entries, the null entry and the filename entry.
1294 if (sym >= GLOBAL_TEMP_BASE)
1295 sym += -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1297 p = entry;
1298 WRITELONG(p, r->address);
1299 WRITELONG(p, (sym << 8) + r->type);
1300 saa_wbytes(s, entry, 8L);
1301 *len += 8;
1303 r = r->next;
1306 return s;
1309 static void elf_section_header(int name, int type, int flags,
1310 void *data, bool is_saa, int32_t datalen,
1311 int link, int info, int align, int eltsize)
1313 elf_sects[elf_nsect].data = data;
1314 elf_sects[elf_nsect].len = datalen;
1315 elf_sects[elf_nsect].is_saa = is_saa;
1316 elf_nsect++;
1318 fwriteint32_t((int32_t)name, elffp);
1319 fwriteint32_t((int32_t)type, elffp);
1320 fwriteint32_t((int32_t)flags, elffp);
1321 fwriteint32_t(0L, elffp); /* no address, ever, in object files */
1322 fwriteint32_t(type == 0 ? 0L : elf_foffs, elffp);
1323 fwriteint32_t(datalen, elffp);
1324 if (data)
1325 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1326 fwriteint32_t((int32_t)link, elffp);
1327 fwriteint32_t((int32_t)info, elffp);
1328 fwriteint32_t((int32_t)align, elffp);
1329 fwriteint32_t((int32_t)eltsize, elffp);
1332 static void elf_write_sections(void)
1334 int i;
1335 for (i = 0; i < elf_nsect; i++)
1336 if (elf_sects[i].data) {
1337 int32_t len = elf_sects[i].len;
1338 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1339 int32_t align = reallen - len;
1340 if (elf_sects[i].is_saa)
1341 saa_fpwrite(elf_sects[i].data, elffp);
1342 else
1343 fwrite(elf_sects[i].data, len, 1, elffp);
1344 fwritezero(align, elffp);
1348 static void elf_sect_write(struct Section *sect,
1349 const uint8_t *data, uint32_t len)
1351 saa_wbytes(sect->data, data, len);
1352 sect->len += len;
1355 static int32_t elf_segbase(int32_t segment)
1357 return segment;
1360 static int elf_directive(char *directive, char *value, int pass)
1362 bool err;
1363 int64_t n;
1364 char *p;
1366 if (!strcmp(directive, "osabi")) {
1367 if (pass == 2)
1368 return 1; /* ignore in pass 2 */
1370 n = readnum(value, &err);
1371 if (err) {
1372 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1373 return 1;
1375 if (n < 0 || n > 255) {
1376 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1377 return 1;
1379 elf_osabi = n;
1380 elf_abiver = 0;
1382 if ((p = strchr(value,',')) == NULL)
1383 return 1;
1385 n = readnum(p+1, &err);
1386 if (err || n < 0 || n > 255) {
1387 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1388 return 1;
1391 elf_abiver = n;
1392 return 1;
1395 return 0;
1398 static void elf_filename(char *inname, char *outname, efunc error)
1400 strcpy(elf_module, inname);
1401 standard_extension(inname, outname, ".o", error);
1404 extern macros_t elf_stdmac[];
1406 static int elf_set_info(enum geninfo type, char **val)
1408 (void)type;
1409 (void)val;
1410 return 0;
1412 static struct dfmt df_dwarf = {
1413 "ELF32 (i386) dwarf debug format for Linux/Unix",
1414 "dwarf",
1415 dwarf32_init,
1416 dwarf32_linenum,
1417 debug32_deflabel,
1418 debug32_directive,
1419 debug32_typevalue,
1420 dwarf32_output,
1421 dwarf32_cleanup
1423 static struct dfmt df_stabs = {
1424 "ELF32 (i386) stabs debug format for Linux/Unix",
1425 "stabs",
1426 stabs32_init,
1427 stabs32_linenum,
1428 debug32_deflabel,
1429 debug32_directive,
1430 debug32_typevalue,
1431 stabs32_output,
1432 stabs32_cleanup
1435 struct dfmt *elf32_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1437 struct ofmt of_elf32 = {
1438 "ELF32 (i386) object files (e.g. Linux)",
1439 "elf32",
1440 NULL,
1441 elf32_debugs_arr,
1442 &df_stabs,
1443 elf_stdmac,
1444 elf_init,
1445 elf_set_info,
1446 elf_out,
1447 elf_deflabel,
1448 elf_section_names,
1449 elf_segbase,
1450 elf_directive,
1451 elf_filename,
1452 elf_cleanup
1455 struct ofmt of_elf = {
1456 "ELF (short name for ELF32) ",
1457 "elf",
1458 NULL,
1459 elf32_debugs_arr,
1460 &df_stabs,
1461 elf_stdmac,
1462 elf_init_hack,
1463 elf_set_info,
1464 elf_out,
1465 elf_deflabel,
1466 elf_section_names,
1467 elf_segbase,
1468 elf_directive,
1469 elf_filename,
1470 elf_cleanup
1472 /* again, the stabs debugging stuff (code) */
1474 void stabs32_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1476 (void)of;
1477 (void)id;
1478 (void)fp;
1479 (void)error;
1482 void stabs32_linenum(const char *filename, int32_t linenumber, 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 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 void debug32_directive(const char *directive, const char *params)
1517 (void)directive;
1518 (void)params;
1521 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 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 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 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 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 void dwarf32_linenum(const char *filename, int32_t linenumber, int32_t segto)
1788 (void)segto;
1789 dwarf32_findfile(filename);
1790 debug_immcall = 1;
1791 currentline = linenumber;
1794 /* called from elf_out with type == TY_DEBUGSYMLIN */
1795 void dwarf32_output(int type, void *param)
1797 int ln, aa, inx, maxln, soc;
1798 struct symlininfo *s;
1799 struct SAA *plinep;
1801 (void)type;
1803 s = (struct symlininfo *)param;
1804 /* line number info is only gathered for executable sections */
1805 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1806 return;
1807 /* Check if section index has changed */
1808 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1810 dwarf32_findsect(s->section);
1812 /* do nothing unless line or file has changed */
1813 if (debug_immcall)
1815 ln = currentline - dwarf_csect->line;
1816 aa = s->offset - dwarf_csect->offset;
1817 inx = dwarf_clist->line;
1818 plinep = dwarf_csect->psaa;
1819 /* check for file change */
1820 if (!(inx == dwarf_csect->file))
1822 saa_write8(plinep,DW_LNS_set_file);
1823 saa_write8(plinep,inx);
1824 dwarf_csect->file = inx;
1826 /* check for line change */
1827 if (ln)
1829 /* test if in range of special op code */
1830 maxln = line_base + line_range;
1831 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1832 if (ln >= line_base && ln < maxln && soc < 256)
1834 saa_write8(plinep,soc);
1836 else
1838 if (ln)
1840 saa_write8(plinep,DW_LNS_advance_line);
1841 saa_wleb128s(plinep,ln);
1843 if (aa)
1845 saa_write8(plinep,DW_LNS_advance_pc);
1846 saa_wleb128u(plinep,aa);
1849 dwarf_csect->line = currentline;
1850 dwarf_csect->offset = s->offset;
1852 /* show change handled */
1853 debug_immcall = 0;
1858 void dwarf32_generate(void)
1860 uint8_t *pbuf;
1861 int indx;
1862 struct linelist *ftentry;
1863 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1864 struct SAA *parangesrel, *plinesrel, *pinforel;
1865 struct sectlist *psect;
1866 size_t saalen, linepoff, totlen, highaddr;
1868 /* write epilogues for each line program range */
1869 /* and build aranges section */
1870 paranges = saa_init(1L);
1871 parangesrel = saa_init(1L);
1872 saa_write16(paranges,2); /* dwarf version */
1873 saa_write32(parangesrel, paranges->datalen+4);
1874 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
1875 saa_write32(parangesrel, 0);
1876 saa_write32(paranges,0); /* offset into info */
1877 saa_write8(paranges,4); /* pointer size */
1878 saa_write8(paranges,0); /* not segmented */
1879 saa_write32(paranges,0); /* padding */
1880 /* iterate though sectlist entries */
1881 psect = dwarf_fsect;
1882 totlen = 0;
1883 highaddr = 0;
1884 for (indx = 0; indx < dwarf_nsections; indx++)
1886 plinep = psect->psaa;
1887 /* Line Number Program Epilogue */
1888 saa_write8(plinep,2); /* std op 2 */
1889 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1890 saa_write8(plinep,DW_LNS_extended_op);
1891 saa_write8(plinep,1); /* operand length */
1892 saa_write8(plinep,DW_LNE_end_sequence);
1893 totlen += plinep->datalen;
1894 /* range table relocation entry */
1895 saa_write32(parangesrel, paranges->datalen + 4);
1896 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
1897 saa_write32(parangesrel, (uint32_t) 0);
1898 /* range table entry */
1899 saa_write32(paranges,0x0000); /* range start */
1900 saa_write32(paranges,sects[psect->section]->len); /* range length */
1901 highaddr += sects[psect->section]->len;
1902 /* done with this entry */
1903 psect = psect->next;
1905 saa_write32(paranges,0); /* null address */
1906 saa_write32(paranges,0); /* null length */
1907 saalen = paranges->datalen;
1908 arangeslen = saalen + 4;
1909 arangesbuf = pbuf = nasm_malloc(arangeslen);
1910 WRITELONG(pbuf,saalen); /* initial length */
1911 saa_rnbytes(paranges, pbuf, saalen);
1912 saa_free(paranges);
1914 /* build rela.aranges section */
1915 arangesrellen = saalen = parangesrel->datalen;
1916 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1917 saa_rnbytes(parangesrel, pbuf, saalen);
1918 saa_free(parangesrel);
1920 /* build pubnames section */
1921 ppubnames = saa_init(1L);
1922 saa_write16(ppubnames,3); /* dwarf version */
1923 saa_write32(ppubnames,0); /* offset into info */
1924 saa_write32(ppubnames,0); /* space used in info */
1925 saa_write32(ppubnames,0); /* end of list */
1926 saalen = ppubnames->datalen;
1927 pubnameslen = saalen + 4;
1928 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1929 WRITELONG(pbuf,saalen); /* initial length */
1930 saa_rnbytes(ppubnames, pbuf, saalen);
1931 saa_free(ppubnames);
1933 /* build info section */
1934 pinfo = saa_init(1L);
1935 pinforel = saa_init(1L);
1936 saa_write16(pinfo,2); /* dwarf version */
1937 saa_write32(pinforel, pinfo->datalen + 4);
1938 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
1939 saa_write32(pinforel, 0);
1940 saa_write32(pinfo,0); /* offset into abbrev */
1941 saa_write8(pinfo,4); /* pointer size */
1942 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1943 saa_write32(pinforel, pinfo->datalen + 4);
1944 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1945 saa_write32(pinforel, 0);
1946 saa_write32(pinfo,0); /* DW_AT_low_pc */
1947 saa_write32(pinforel, pinfo->datalen + 4);
1948 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1949 saa_write32(pinforel, 0);
1950 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
1951 saa_write32(pinforel, pinfo->datalen + 4);
1952 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
1953 saa_write32(pinforel, 0);
1954 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1955 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1956 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1957 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1958 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1959 saa_write32(pinforel, pinfo->datalen + 4);
1960 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1961 saa_write32(pinforel, 0);
1962 saa_write32(pinfo,0); /* DW_AT_low_pc */
1963 saa_write32(pinfo,0); /* DW_AT_frame_base */
1964 saa_write8(pinfo,0); /* end of entries */
1965 saalen = pinfo->datalen;
1966 infolen = saalen + 4;
1967 infobuf = pbuf = nasm_malloc(infolen);
1968 WRITELONG(pbuf,saalen); /* initial length */
1969 saa_rnbytes(pinfo, pbuf, saalen);
1970 saa_free(pinfo);
1972 /* build rela.info section */
1973 inforellen = saalen = pinforel->datalen;
1974 inforelbuf = pbuf = nasm_malloc(inforellen);
1975 saa_rnbytes(pinforel, pbuf, saalen);
1976 saa_free(pinforel);
1978 /* build abbrev section */
1979 pabbrev = saa_init(1L);
1980 saa_write8(pabbrev,1); /* entry number LEB128u */
1981 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
1982 saa_write8(pabbrev,1); /* has children */
1983 /* the following attributes and forms are all LEB128u values */
1984 saa_write8(pabbrev,DW_AT_low_pc);
1985 saa_write8(pabbrev,DW_FORM_addr);
1986 saa_write8(pabbrev,DW_AT_high_pc);
1987 saa_write8(pabbrev,DW_FORM_addr);
1988 saa_write8(pabbrev,DW_AT_stmt_list);
1989 saa_write8(pabbrev,DW_FORM_data4);
1990 saa_write8(pabbrev,DW_AT_name);
1991 saa_write8(pabbrev,DW_FORM_string);
1992 saa_write8(pabbrev,DW_AT_producer);
1993 saa_write8(pabbrev,DW_FORM_string);
1994 saa_write8(pabbrev,DW_AT_language);
1995 saa_write8(pabbrev,DW_FORM_data2);
1996 saa_write16(pabbrev,0); /* end of entry */
1997 /* LEB128u usage same as above */
1998 saa_write8(pabbrev,2); /* entry number */
1999 saa_write8(pabbrev,DW_TAG_subprogram);
2000 saa_write8(pabbrev,0); /* no children */
2001 saa_write8(pabbrev,DW_AT_low_pc);
2002 saa_write8(pabbrev,DW_FORM_addr);
2003 saa_write8(pabbrev,DW_AT_frame_base);
2004 saa_write8(pabbrev,DW_FORM_data4);
2005 saa_write16(pabbrev,0); /* end of entry */
2006 abbrevlen = saalen = pabbrev->datalen;
2007 abbrevbuf = pbuf = nasm_malloc(saalen);
2008 saa_rnbytes(pabbrev, pbuf, saalen);
2009 saa_free(pabbrev);
2011 /* build line section */
2012 /* prolog */
2013 plines = saa_init(1L);
2014 saa_write8(plines,1); /* Minimum Instruction Length */
2015 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2016 saa_write8(plines,line_base); /* Line Base */
2017 saa_write8(plines,line_range); /* Line Range */
2018 saa_write8(plines,opcode_base); /* Opcode Base */
2019 /* standard opcode lengths (# of LEB128u operands) */
2020 saa_write8(plines,0); /* Std opcode 1 length */
2021 saa_write8(plines,1); /* Std opcode 2 length */
2022 saa_write8(plines,1); /* Std opcode 3 length */
2023 saa_write8(plines,1); /* Std opcode 4 length */
2024 saa_write8(plines,1); /* Std opcode 5 length */
2025 saa_write8(plines,0); /* Std opcode 6 length */
2026 saa_write8(plines,0); /* Std opcode 7 length */
2027 saa_write8(plines,0); /* Std opcode 8 length */
2028 saa_write8(plines,1); /* Std opcode 9 length */
2029 saa_write8(plines,0); /* Std opcode 10 length */
2030 saa_write8(plines,0); /* Std opcode 11 length */
2031 saa_write8(plines,1); /* Std opcode 12 length */
2032 /* Directory Table */
2033 saa_write8(plines,0); /* End of table */
2034 /* File Name Table */
2035 ftentry = dwarf_flist;
2036 for (indx = 0;indx<dwarf_numfiles;indx++)
2038 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2039 saa_write8(plines,0); /* directory LEB128u */
2040 saa_write8(plines,0); /* time LEB128u */
2041 saa_write8(plines,0); /* size LEB128u */
2042 ftentry = ftentry->next;
2044 saa_write8(plines,0); /* End of table */
2045 linepoff = plines->datalen;
2046 linelen = linepoff + totlen + 10;
2047 linebuf = pbuf = nasm_malloc(linelen);
2048 WRITELONG(pbuf,linelen-4); /* initial length */
2049 WRITESHORT(pbuf,3); /* dwarf version */
2050 WRITELONG(pbuf,linepoff); /* offset to line number program */
2051 /* write line header */
2052 saalen = linepoff;
2053 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2054 pbuf += linepoff;
2055 saa_free(plines);
2056 /* concatonate line program ranges */
2057 linepoff += 13;
2058 plinesrel = saa_init(1L);
2059 psect = dwarf_fsect;
2060 for (indx = 0; indx < dwarf_nsections; indx++)
2062 saa_write32(plinesrel, linepoff);
2063 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
2064 saa_write32(plinesrel, (uint32_t) 0);
2065 plinep = psect->psaa;
2066 saalen = plinep->datalen;
2067 saa_rnbytes(plinep, pbuf, saalen);
2068 pbuf += saalen;
2069 linepoff += saalen;
2070 saa_free(plinep);
2071 /* done with this entry */
2072 psect = psect->next;
2076 /* build rela.lines section */
2077 linerellen =saalen = plinesrel->datalen;
2078 linerelbuf = pbuf = nasm_malloc(linerellen);
2079 saa_rnbytes(plinesrel, pbuf, saalen);
2080 saa_free(plinesrel);
2082 /* build frame section */
2083 framelen = 4;
2084 framebuf = pbuf = nasm_malloc(framelen);
2085 WRITELONG(pbuf,framelen-4); /* initial length */
2087 /* build loc section */
2088 loclen = 16;
2089 locbuf = pbuf = nasm_malloc(loclen);
2090 WRITELONG(pbuf,0); /* null beginning offset */
2091 WRITELONG(pbuf,0); /* null ending offset */
2094 void dwarf32_cleanup(void)
2096 if (arangesbuf)
2097 nasm_free(arangesbuf);
2098 if (arangesrelbuf)
2099 nasm_free(arangesrelbuf);
2100 if (pubnamesbuf)
2101 nasm_free(pubnamesbuf);
2102 if (infobuf)
2103 nasm_free(infobuf);
2104 if (inforelbuf)
2105 nasm_free(inforelbuf);
2106 if (abbrevbuf)
2107 nasm_free(abbrevbuf);
2108 if (linebuf)
2109 nasm_free(linebuf);
2110 if (linerelbuf)
2111 nasm_free(linerelbuf);
2112 if (framebuf)
2113 nasm_free(framebuf);
2114 if (locbuf)
2115 nasm_free(locbuf);
2117 void dwarf32_findfile(const char * fname)
2119 int finx;
2120 struct linelist *match;
2122 /* return if fname is current file name */
2123 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2124 /* search for match */
2125 else
2127 match = 0;
2128 if (dwarf_flist)
2130 match = dwarf_flist;
2131 for (finx = 0; finx < dwarf_numfiles; finx++)
2133 if (!(strcmp(fname, match->filename)))
2135 dwarf_clist = match;
2136 return;
2140 /* add file name to end of list */
2141 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2142 dwarf_numfiles++;
2143 dwarf_clist->line = dwarf_numfiles;
2144 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2145 strcpy(dwarf_clist->filename,fname);
2146 dwarf_clist->next = 0;
2147 /* if first entry */
2148 if (!dwarf_flist)
2150 dwarf_flist = dwarf_elist = dwarf_clist;
2151 dwarf_clist->last = 0;
2153 /* chain to previous entry */
2154 else
2156 dwarf_elist->next = dwarf_clist;
2157 dwarf_elist = dwarf_clist;
2161 /* */
2162 void dwarf32_findsect(const int index)
2164 int sinx;
2165 struct sectlist *match;
2166 struct SAA *plinep;
2167 /* return if index is current section index */
2168 if (dwarf_csect && (dwarf_csect->section == index))
2170 return;
2172 /* search for match */
2173 else
2175 match = 0;
2176 if (dwarf_fsect)
2178 match = dwarf_fsect;
2179 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2181 if ((match->section == index))
2183 dwarf_csect = match;
2184 return;
2186 match = match->next;
2189 /* add entry to end of list */
2190 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2191 dwarf_nsections++;
2192 dwarf_csect->psaa = plinep = saa_init(1L);
2193 dwarf_csect->line = 1;
2194 dwarf_csect->offset = 0;
2195 dwarf_csect->file = 1;
2196 dwarf_csect->section = index;
2197 dwarf_csect->next = 0;
2198 /* set relocatable address at start of line program */
2199 saa_write8(plinep,DW_LNS_extended_op);
2200 saa_write8(plinep,5); /* operand length */
2201 saa_write8(plinep,DW_LNE_set_address);
2202 saa_write32(plinep,0); /* Start Address */
2203 /* if first entry */
2204 if (!dwarf_fsect)
2206 dwarf_fsect = dwarf_esect = dwarf_csect;
2207 dwarf_csect->last = 0;
2209 /* chain to previous entry */
2210 else
2212 dwarf_esect->next = dwarf_csect;
2213 dwarf_esect = dwarf_csect;
2218 #endif /* OF_ELF */