Handle the new ELF headers in the dependency generation
[nasm/nasm.git] / output / outelf32.c
blob6e4b20d03156af70267477ac2c66ba38b7283843
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;
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 int symtabsection;
166 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
167 static int stablen, stabstrlen, stabrellen;
169 /* dwarf debug variables */
170 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
171 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
172 static int dwarf_numfiles = 0, dwarf_nsections;
173 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
174 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
175 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
176 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
177 abbrevlen, linelen, linerellen, framelen, loclen;
178 static int32_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
180 static struct dfmt df_dwarf;
181 static struct dfmt df_stabs;
182 static struct Symbol *lastsym;
184 /* common debugging routines */
185 void debug32_typevalue(int32_t);
186 void debug32_init(struct ofmt *, void *, FILE *, efunc);
187 void debug32_deflabel(char *, int32_t, int64_t, int, char *);
188 void debug32_directive(const char *, const char *);
190 /* stabs debugging routines */
191 void stabs32_linenum(const char *filename, int32_t linenumber, int32_t);
192 void stabs32_output(int, void *);
193 void stabs32_generate(void);
194 void stabs32_cleanup(void);
196 /* dwarf debugging routines */
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 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 = 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 =
330 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
331 sects[nsects++] = s;
333 return nsects - 1;
337 static int32_t elf_section_names(char *name, int pass, int *bits)
339 char *p;
340 uint32_t flags, flags_and, flags_or;
341 uint32_t align;
342 int type, i;
345 * Default is 32 bits.
347 if (!name) {
348 *bits = 32;
349 return def_seg;
352 p = name;
353 while (*p && !nasm_isspace(*p))
354 p++;
355 if (*p)
356 *p++ = '\0';
357 flags_and = flags_or = type = align = 0;
359 while (*p && nasm_isspace(*p))
360 p++;
361 while (*p) {
362 char *q = p;
363 while (*p && !nasm_isspace(*p))
364 p++;
365 if (*p)
366 *p++ = '\0';
367 while (*p && nasm_isspace(*p))
368 p++;
370 if (!nasm_strnicmp(q, "align=", 6)) {
371 align = atoi(q + 6);
372 if (align == 0)
373 align = 1;
374 if ((align - 1) & align) { /* means it's not a power of two */
375 error(ERR_NONFATAL, "section alignment %d is not"
376 " a power of two", align);
377 align = 1;
379 } else if (!nasm_stricmp(q, "alloc")) {
380 flags_and |= SHF_ALLOC;
381 flags_or |= SHF_ALLOC;
382 } else if (!nasm_stricmp(q, "noalloc")) {
383 flags_and |= SHF_ALLOC;
384 flags_or &= ~SHF_ALLOC;
385 } else if (!nasm_stricmp(q, "exec")) {
386 flags_and |= SHF_EXECINSTR;
387 flags_or |= SHF_EXECINSTR;
388 } else if (!nasm_stricmp(q, "noexec")) {
389 flags_and |= SHF_EXECINSTR;
390 flags_or &= ~SHF_EXECINSTR;
391 } else if (!nasm_stricmp(q, "write")) {
392 flags_and |= SHF_WRITE;
393 flags_or |= SHF_WRITE;
394 } else if (!nasm_stricmp(q, "tls")) {
395 flags_and |= SHF_TLS;
396 flags_or |= SHF_TLS;
397 } else if (!nasm_stricmp(q, "nowrite")) {
398 flags_and |= SHF_WRITE;
399 flags_or &= ~SHF_WRITE;
400 } else if (!nasm_stricmp(q, "progbits")) {
401 type = SHT_PROGBITS;
402 } else if (!nasm_stricmp(q, "nobits")) {
403 type = SHT_NOBITS;
404 } else if (pass == 1) error(ERR_WARNING, "Unknown section attribute '%s' ignored on"
405 " declaration of section `%s'", q, name);
408 if (!strcmp(name, ".shstrtab") ||
409 !strcmp(name, ".symtab") ||
410 !strcmp(name, ".strtab")) {
411 error(ERR_NONFATAL, "attempt to redefine reserved section"
412 "name `%s'", name);
413 return NO_SEG;
416 for (i = 0; i < nsects; i++)
417 if (!strcmp(name, sects[i]->name))
418 break;
419 if (i == nsects) {
420 const struct elf_known_section *ks = elf_known_sections;
422 while (ks->name) {
423 if (!strcmp(name, ks->name))
424 break;
425 ks++;
428 type = type ? type : ks->type;
429 align = align ? align : ks->align;
430 flags = (ks->flags & ~flags_and) | flags_or;
432 i = elf_make_section(name, type, flags, align);
433 } else if (pass == 1) {
434 if ((type && sects[i]->type != type)
435 || (align && sects[i]->align != align)
436 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
437 error(ERR_WARNING, "section attributes ignored on"
438 " redeclaration of section `%s'", name);
441 return sects[i]->index;
444 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
445 int is_global, char *special)
447 int pos = strslen;
448 struct Symbol *sym;
449 bool special_used = false;
451 #if defined(DEBUG) && DEBUG>2
452 fprintf(stderr,
453 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
454 name, segment, offset, is_global, special);
455 #endif
456 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
458 * This is a NASM special symbol. We never allow it into
459 * the ELF symbol table, even if it's a valid one. If it
460 * _isn't_ a valid one, we should barf immediately.
462 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
463 strcmp(name, "..got") && strcmp(name, "..plt") &&
464 strcmp(name, "..sym") && strcmp(name, "..tlsie"))
465 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
466 return;
469 if (is_global == 3) {
470 struct Symbol **s;
472 * Fix up a forward-reference symbol size from the first
473 * pass.
475 for (s = &fwds; *s; s = &(*s)->nextfwd)
476 if (!strcmp((*s)->name, name)) {
477 struct tokenval tokval;
478 expr *e;
479 char *p = special;
481 while (*p && !nasm_isspace(*p))
482 p++;
483 while (*p && nasm_isspace(*p))
484 p++;
485 stdscan_reset();
486 stdscan_bufptr = p;
487 tokval.t_type = TOKEN_INVALID;
488 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
489 if (e) {
490 if (!is_simple(e))
491 error(ERR_NONFATAL, "cannot use relocatable"
492 " expression as symbol size");
493 else
494 (*s)->size = reloc_value(e);
498 * Remove it from the list of unresolved sizes.
500 nasm_free((*s)->name);
501 *s = (*s)->nextfwd;
502 return;
504 return; /* it wasn't an important one */
507 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
508 strslen += 1 + strlen(name);
510 lastsym = sym = saa_wstruct(syms);
512 memset(&sym->symv, 0, sizeof(struct rbtree));
514 sym->strpos = pos;
515 sym->type = is_global ? SYM_GLOBAL : 0;
516 sym->other = STV_DEFAULT;
517 sym->size = 0;
518 if (segment == NO_SEG)
519 sym->section = SHN_ABS;
520 else {
521 int i;
522 sym->section = SHN_UNDEF;
523 if (nsects == 0 && segment == def_seg) {
524 int tempint;
525 if (segment != elf_section_names(".text", 2, &tempint))
526 error(ERR_PANIC,
527 "strange segment conditions in ELF driver");
528 sym->section = nsects;
529 } else {
530 for (i = 0; i < nsects; i++)
531 if (segment == sects[i]->index) {
532 sym->section = i + 1;
533 break;
538 if (is_global == 2) {
539 sym->size = offset;
540 sym->symv.key = 0;
541 sym->section = SHN_COMMON;
543 * We have a common variable. Check the special text to see
544 * if it's a valid number and power of two; if so, store it
545 * as the alignment for the common variable.
547 if (special) {
548 bool err;
549 sym->symv.key = readnum(special, &err);
550 if (err)
551 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
552 " valid number", special);
553 else if ((sym->symv.key | (sym->symv.key - 1))
554 != 2 * sym->symv.key - 1)
555 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
556 " power of two", special);
558 special_used = true;
559 } else
560 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
562 if (sym->type == SYM_GLOBAL) {
564 * If sym->section == SHN_ABS, then the first line of the
565 * else section would cause a core dump, because its a reference
566 * beyond the end of the section array.
567 * This behaviour is exhibited by this code:
568 * GLOBAL crash_nasm
569 * crash_nasm equ 0
570 * To avoid such a crash, such requests are silently discarded.
571 * This may not be the best solution.
573 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
574 bsym = raa_write(bsym, segment, nglobs);
575 } else if (sym->section != SHN_ABS) {
577 * This is a global symbol; so we must add it to the rbtree
578 * of global symbols in its section.
580 * In addition, we check the special text for symbol
581 * type and size information.
583 sects[sym->section-1]->gsyms =
584 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
586 if (special) {
587 int n = strcspn(special, " \t");
589 if (!nasm_strnicmp(special, "function", n))
590 sym->type |= STT_FUNC;
591 else if (!nasm_strnicmp(special, "data", n) ||
592 !nasm_strnicmp(special, "object", n))
593 sym->type |= STT_OBJECT;
594 else if (!nasm_strnicmp(special, "notype", n))
595 sym->type |= STT_NOTYPE;
596 else
597 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
598 n, special);
599 special += n;
601 while (nasm_isspace(*special))
602 ++special;
603 if (*special) {
604 n = strcspn(special, " \t");
605 if (!nasm_strnicmp(special, "default", n))
606 sym->other = STV_DEFAULT;
607 else if (!nasm_strnicmp(special, "internal", n))
608 sym->other = STV_INTERNAL;
609 else if (!nasm_strnicmp(special, "hidden", n))
610 sym->other = STV_HIDDEN;
611 else if (!nasm_strnicmp(special, "protected", n))
612 sym->other = STV_PROTECTED;
613 else
614 n = 0;
615 special += n;
618 if (*special) {
619 struct tokenval tokval;
620 expr *e;
621 int fwd = 0;
622 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
624 while (special[n] && nasm_isspace(special[n]))
625 n++;
627 * We have a size expression; attempt to
628 * evaluate it.
630 stdscan_reset();
631 stdscan_bufptr = special + n;
632 tokval.t_type = TOKEN_INVALID;
633 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
634 NULL);
635 if (fwd) {
636 sym->nextfwd = fwds;
637 fwds = sym;
638 sym->name = nasm_strdup(name);
639 } else if (e) {
640 if (!is_simple(e))
641 error(ERR_NONFATAL, "cannot use relocatable"
642 " expression as symbol size");
643 else
644 sym->size = reloc_value(e);
646 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
648 special_used = true;
651 * If TLS segment, mark symbol accordingly.
653 if (sects[sym->section - 1]->flags & SHF_TLS) {
654 sym->type &= 0xf0;
655 sym->type |= STT_TLS;
658 sym->globnum = nglobs;
659 nglobs++;
660 } else
661 nlocals++;
663 if (special && !special_used)
664 error(ERR_NONFATAL, "no special symbol features supported here");
667 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
669 struct Reloc *r;
671 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
672 sect->tail = &r->next;
673 r->next = NULL;
675 r->address = sect->len;
676 if (segment == NO_SEG)
677 r->symbol = 0;
678 else {
679 int i;
680 r->symbol = 0;
681 for (i = 0; i < nsects; i++)
682 if (segment == sects[i]->index)
683 r->symbol = i + 2;
684 if (!r->symbol)
685 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
687 r->type = type;
689 sect->nrelocs++;
693 * This routine deals with ..got and ..sym relocations: the more
694 * complicated kinds. In shared-library writing, some relocations
695 * with respect to global symbols must refer to the precise symbol
696 * rather than referring to an offset from the base of the section
697 * _containing_ the symbol. Such relocations call to this routine,
698 * which searches the symbol list for the symbol in question.
700 * R_386_GOT32 references require the _exact_ symbol address to be
701 * used; R_386_32 references can be at an offset from the symbol.
702 * The boolean argument `exact' tells us this.
704 * Return value is the adjusted value of `addr', having become an
705 * offset from the symbol rather than the section. Should always be
706 * zero when returning from an exact call.
708 * Limitation: if you define two symbols at the same place,
709 * confusion will occur.
711 * Inefficiency: we search, currently, using a linked list which
712 * isn't even necessarily sorted.
714 static int32_t elf_add_gsym_reloc(struct Section *sect,
715 int32_t segment, uint32_t offset,
716 int type, bool exact)
718 struct Reloc *r;
719 struct Section *s;
720 struct Symbol *sym;
721 struct rbtree *srb;
722 int i;
725 * First look up the segment/offset pair and find a global
726 * symbol corresponding to it. If it's not one of our segments,
727 * then it must be an external symbol, in which case we're fine
728 * doing a normal elf_add_reloc after first sanity-checking
729 * that the offset from the symbol is zero.
731 s = NULL;
732 for (i = 0; i < nsects; i++)
733 if (segment == sects[i]->index) {
734 s = sects[i];
735 break;
737 if (!s) {
738 if (exact && offset != 0)
739 error(ERR_NONFATAL, "unable to find a suitable global symbol"
740 " for this reference");
741 else
742 elf_add_reloc(sect, segment, type);
743 return offset;
746 srb = rb_search(s->gsyms, offset);
747 if (!srb || (exact && srb->key != offset)) {
748 error(ERR_NONFATAL, "unable to find a suitable global symbol"
749 " for this reference");
750 return 0;
752 sym = container_of(srb, struct Symbol, symv);
754 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
755 sect->tail = &r->next;
756 r->next = NULL;
758 r->address = sect->len;
759 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
760 r->type = type;
762 sect->nrelocs++;
764 return offset - sym->symv.key;
767 static void elf_out(int32_t segto, const void *data,
768 enum out_type type, uint64_t size,
769 int32_t segment, int32_t wrt)
771 struct Section *s;
772 int32_t addr;
773 uint8_t mydata[4], *p;
774 int i;
775 static struct symlininfo sinfo;
778 * handle absolute-assembly (structure definitions)
780 if (segto == NO_SEG) {
781 if (type != OUT_RESERVE)
782 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
783 " space");
784 return;
787 s = NULL;
788 for (i = 0; i < nsects; i++)
789 if (segto == sects[i]->index) {
790 s = sects[i];
791 break;
793 if (!s) {
794 int tempint; /* ignored */
795 if (segto != elf_section_names(".text", 2, &tempint))
796 error(ERR_PANIC, "strange segment conditions in ELF driver");
797 else {
798 s = sects[nsects - 1];
799 i = nsects - 1;
803 /* again some stabs debugging stuff */
804 if (of_elf32.current_dfmt) {
805 sinfo.offset = s->len;
806 sinfo.section = i;
807 sinfo.name = s->name;
808 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
810 /* end of debugging stuff */
812 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
813 error(ERR_WARNING, "attempt to initialize memory in"
814 " BSS section `%s': ignored", s->name);
815 s->len += realsize(type, size);
816 return;
819 if (type == OUT_RESERVE) {
820 if (s->type == SHT_PROGBITS) {
821 error(ERR_WARNING, "uninitialized space declared in"
822 " non-BSS section `%s': zeroing", s->name);
823 elf_sect_write(s, NULL, size);
824 } else
825 s->len += size;
826 } else if (type == OUT_RAWDATA) {
827 if (segment != NO_SEG)
828 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
829 elf_sect_write(s, data, size);
830 } else if (type == OUT_ADDRESS) {
831 bool gnu16 = false;
832 addr = *(int64_t *)data;
833 if (segment != NO_SEG) {
834 if (segment % 2) {
835 error(ERR_NONFATAL, "ELF format does not support"
836 " segment base references");
837 } else {
838 if (wrt == NO_SEG) {
839 if (size == 2) {
840 gnu16 = true;
841 elf_add_reloc(s, segment, R_386_16);
842 } else {
843 elf_add_reloc(s, segment, R_386_32);
845 } else if (wrt == elf_gotpc_sect + 1) {
847 * The user will supply GOT relative to $$. ELF
848 * will let us have GOT relative to $. So we
849 * need to fix up the data item by $-$$.
851 addr += s->len;
852 elf_add_reloc(s, segment, R_386_GOTPC);
853 } else if (wrt == elf_gotoff_sect + 1) {
854 elf_add_reloc(s, segment, R_386_GOTOFF);
855 } else if (wrt == elf_tlsie_sect + 1) {
856 addr = elf_add_gsym_reloc(s, segment, addr,
857 R_386_TLS_IE, true);
858 } else if (wrt == elf_got_sect + 1) {
859 addr = elf_add_gsym_reloc(s, segment, addr,
860 R_386_GOT32, true);
861 } else if (wrt == elf_sym_sect + 1) {
862 if (size == 2) {
863 gnu16 = true;
864 addr = elf_add_gsym_reloc(s, segment, addr,
865 R_386_16, false);
866 } else {
867 addr = elf_add_gsym_reloc(s, segment, addr,
868 R_386_32, false);
870 } else if (wrt == elf_plt_sect + 1) {
871 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
872 "relative PLT references");
873 } else {
874 error(ERR_NONFATAL, "ELF format does not support this"
875 " use of WRT");
876 wrt = NO_SEG; /* we can at least _try_ to continue */
880 p = mydata;
881 if (gnu16) {
882 error(ERR_WARNING | ERR_WARN_GNUELF,
883 "16-bit relocations in ELF is a GNU extension");
884 WRITESHORT(p, addr);
885 } else {
886 if (size != 4 && segment != NO_SEG) {
887 error(ERR_NONFATAL,
888 "Unsupported non-32-bit ELF relocation");
890 WRITELONG(p, addr);
892 elf_sect_write(s, mydata, size);
893 } else if (type == OUT_REL2ADR) {
894 if (segment == segto)
895 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
896 if (segment != NO_SEG && segment % 2) {
897 error(ERR_NONFATAL, "ELF format does not support"
898 " segment base references");
899 } else {
900 if (wrt == NO_SEG) {
901 error(ERR_WARNING | ERR_WARN_GNUELF,
902 "16-bit relocations in ELF is a GNU extension");
903 elf_add_reloc(s, segment, R_386_PC16);
904 } else {
905 error(ERR_NONFATAL,
906 "Unsupported non-32-bit ELF relocation");
909 p = mydata;
910 WRITESHORT(p, *(int64_t *)data - size);
911 elf_sect_write(s, mydata, 2L);
912 } else if (type == OUT_REL4ADR) {
913 if (segment == segto)
914 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
915 if (segment != NO_SEG && segment % 2) {
916 error(ERR_NONFATAL, "ELF format does not support"
917 " segment base references");
918 } else {
919 if (wrt == NO_SEG) {
920 elf_add_reloc(s, segment, R_386_PC32);
921 } else if (wrt == elf_plt_sect + 1) {
922 elf_add_reloc(s, segment, R_386_PLT32);
923 } else if (wrt == elf_gotpc_sect + 1 ||
924 wrt == elf_gotoff_sect + 1 ||
925 wrt == elf_got_sect + 1) {
926 error(ERR_NONFATAL, "ELF format cannot produce PC-"
927 "relative GOT references");
928 } else {
929 error(ERR_NONFATAL, "ELF format does not support this"
930 " use of WRT");
931 wrt = NO_SEG; /* we can at least _try_ to continue */
934 p = mydata;
935 WRITELONG(p, *(int64_t *)data - size);
936 elf_sect_write(s, mydata, 4L);
940 static void elf_write(void)
942 int align;
943 int scount;
944 char *p;
945 int i;
947 struct SAA *symtab;
948 int32_t symtablen, symtablocal;
951 * Work out how many sections we will have. We have SHN_UNDEF,
952 * then the flexible user sections, then the fixed sections
953 * `.shstrtab', `.symtab' and `.strtab', then optionally
954 * relocation sections for the user sections.
956 nsections = 4;
957 if (of_elf32.current_dfmt == &df_stabs)
958 nsections += 3;
959 else if (of_elf32.current_dfmt == &df_dwarf)
960 nsections += 10;
962 add_sectname("", ".shstrtab");
963 add_sectname("", ".symtab");
964 add_sectname("", ".strtab");
965 for (i = 0; i < nsects; i++) {
966 nsections++; /* for the section itself */
967 if (sects[i]->head) {
968 nsections++; /* for its relocations */
969 add_sectname(".rel", sects[i]->name);
973 if (of_elf32.current_dfmt == &df_stabs) {
974 /* in case the debug information is wanted, just add these three sections... */
975 add_sectname("", ".stab");
976 add_sectname("", ".stabstr");
977 add_sectname(".rel", ".stab");
980 else if (of_elf32.current_dfmt == &df_dwarf) {
981 /* the dwarf debug standard specifies the following ten sections,
982 not all of which are currently implemented,
983 although all of them are defined. */
984 #define debug_aranges (int32_t) (nsections-10)
985 #define debug_info (int32_t) (nsections-7)
986 #define debug_abbrev (int32_t) (nsections-5)
987 #define debug_line (int32_t) (nsections-4)
988 add_sectname("", ".debug_aranges");
989 add_sectname(".rela", ".debug_aranges");
990 add_sectname("", ".debug_pubnames");
991 add_sectname("", ".debug_info");
992 add_sectname(".rela", ".debug_info");
993 add_sectname("", ".debug_abbrev");
994 add_sectname("", ".debug_line");
995 add_sectname(".rela", ".debug_line");
996 add_sectname("", ".debug_frame");
997 add_sectname("", ".debug_loc");
1001 * Output the ELF header.
1003 fwrite("\177ELF\1\1\1", 7, 1, elffp);
1004 fputc(elf_osabi, elffp);
1005 fputc(elf_abiver, elffp);
1006 fwritezero(7, elffp);
1007 fwriteint16_t(1, elffp); /* ET_REL relocatable file */
1008 fwriteint16_t(3, elffp); /* EM_386 processor ID */
1009 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1010 fwriteint32_t(0L, elffp); /* no entry point */
1011 fwriteint32_t(0L, elffp); /* no program header table */
1012 fwriteint32_t(0x40L, elffp); /* section headers straight after
1013 * ELF header plus alignment */
1014 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1015 fwriteint16_t(0x34, elffp); /* size of ELF header */
1016 fwriteint16_t(0, elffp); /* no program header table, again */
1017 fwriteint16_t(0, elffp); /* still no program header table */
1018 fwriteint16_t(0x28, elffp); /* size of section header */
1019 fwriteint16_t(nsections, elffp); /* number of sections */
1020 fwriteint16_t(nsects + 2, elffp); /* string table section index for
1021 * section header table */
1022 fwriteint32_t(0L, elffp); /* align to 0x40 bytes */
1023 fwriteint32_t(0L, elffp);
1024 fwriteint32_t(0L, elffp);
1027 * Build the symbol table and relocation tables.
1029 symtab = elf_build_symtab(&symtablen, &symtablocal);
1030 for (i = 0; i < nsects; i++)
1031 if (sects[i]->head)
1032 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1033 sects[i]->head);
1036 * Now output the section header table.
1039 elf_foffs = 0x40 + 0x28 * nsections;
1040 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1041 elf_foffs += align;
1042 elf_nsect = 0;
1043 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1045 elf_section_header(0, 0, 0, NULL, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1046 scount = 1; /* needed for the stabs debugging to track the symtable section */
1047 p = shstrtab + 1;
1048 for (i = 0; i < nsects; i++) {
1049 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1050 (sects[i]->type == SHT_PROGBITS ?
1051 sects[i]->data : NULL), true,
1052 sects[i]->len, 0, 0, sects[i]->align, 0);
1053 p += strlen(p) + 1;
1054 scount++; /* dito */
1056 elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1057 scount++; /* dito */
1058 p += strlen(p) + 1;
1059 elf_section_header(p - shstrtab, 2, 0, symtab, true, symtablen, nsects + 4, symtablocal, 4, 16); /* .symtab */
1060 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1061 p += strlen(p) + 1;
1062 elf_section_header(p - shstrtab, 3, 0, strs, true, strslen, 0, 0, 1, 0); /* .strtab */
1063 for (i = 0; i < nsects; i++)
1064 if (sects[i]->head) {
1065 p += strlen(p) + 1;
1066 elf_section_header(p - shstrtab, 9, 0, sects[i]->rel, true,
1067 sects[i]->rellen, nsects + 3, i + 1, 4, 8);
1069 if (of_elf32.current_dfmt == &df_stabs) {
1070 /* for debugging information, create the last three sections
1071 which are the .stab , .stabstr and .rel.stab sections respectively */
1073 /* this function call creates the stab sections in memory */
1074 stabs32_generate();
1076 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1077 p += strlen(p) + 1;
1078 elf_section_header(p - shstrtab, 1, 0, stabbuf, false, stablen,
1079 nsections - 2, 0, 4, 12);
1081 p += strlen(p) + 1;
1082 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, false,
1083 stabstrlen, 0, 0, 4, 0);
1085 p += strlen(p) + 1;
1086 /* link -> symtable info -> section to refer to */
1087 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, false,
1088 stabrellen, symtabsection, nsections - 3, 4,
1092 else if (of_elf32.current_dfmt == &df_dwarf) {
1093 /* for dwarf debugging information, create the ten dwarf sections */
1095 /* this function call creates the dwarf sections in memory */
1096 if (dwarf_fsect) dwarf32_generate();
1098 p += strlen(p) + 1;
1099 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1100 arangeslen, 0, 0, 1, 0);
1101 p += strlen(p) + 1;
1102 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1103 arangesrellen, symtabsection, debug_aranges, 1, 12);
1104 p += strlen(p) + 1;
1105 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1106 pubnameslen, 0, 0, 1, 0);
1107 p += strlen(p) + 1;
1108 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1109 infolen, 0, 0, 1, 0);
1110 p += strlen(p) + 1;
1111 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1112 inforellen, symtabsection, debug_info, 1, 12);
1113 p += strlen(p) + 1;
1114 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1115 abbrevlen, 0, 0, 1, 0);
1116 p += strlen(p) + 1;
1117 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1118 linelen, 0, 0, 1, 0);
1119 p += strlen(p) + 1;
1120 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1121 linerellen, symtabsection, debug_line, 1, 12);
1122 p += strlen(p) + 1;
1123 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1124 framelen, 0, 0, 8, 0);
1125 p += strlen(p) + 1;
1126 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1127 loclen, 0, 0, 1, 0);
1130 fwritezero(align, elffp);
1133 * Now output the sections.
1135 elf_write_sections();
1137 nasm_free(elf_sects);
1138 saa_free(symtab);
1141 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1143 struct SAA *s = saa_init(1L);
1144 struct Symbol *sym;
1145 uint8_t entry[16], *p;
1146 int i;
1148 *len = *local = 0;
1151 * First, an all-zeros entry, required by the ELF spec.
1153 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1154 *len += 16;
1155 (*local)++;
1158 * Next, an entry for the file name.
1160 p = entry;
1161 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1162 WRITELONG(p, 0); /* no value */
1163 WRITELONG(p, 0); /* no size either */
1164 WRITESHORT(p, STT_FILE); /* type FILE */
1165 WRITESHORT(p, SHN_ABS);
1166 saa_wbytes(s, entry, 16L);
1167 *len += 16;
1168 (*local)++;
1171 * Now some standard symbols defining the segments, for relocation
1172 * purposes.
1174 for (i = 1; i <= nsects; i++) {
1175 p = entry;
1176 WRITELONG(p, 0); /* no symbol name */
1177 WRITELONG(p, 0); /* offset zero */
1178 WRITELONG(p, 0); /* size zero */
1179 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1180 WRITESHORT(p, i); /* section id */
1181 saa_wbytes(s, entry, 16L);
1182 *len += 16;
1183 (*local)++;
1187 * Now the other local symbols.
1189 saa_rewind(syms);
1190 while ((sym = saa_rstruct(syms))) {
1191 if (sym->type & SYM_GLOBAL)
1192 continue;
1193 p = entry;
1194 WRITELONG(p, sym->strpos);
1195 WRITELONG(p, sym->symv.key);
1196 WRITELONG(p, sym->size);
1197 WRITECHAR(p, sym->type); /* type and binding */
1198 WRITECHAR(p, sym->other); /* visibility */
1199 WRITESHORT(p, sym->section);
1200 saa_wbytes(s, entry, 16L);
1201 *len += 16;
1202 (*local)++;
1205 * dwarf needs symbols for debug sections
1206 * which are relocation targets.
1208 //*** fix for 32 bit
1209 if (of_elf32.current_dfmt == &df_dwarf) {
1210 dwarf_infosym = *local;
1211 p = entry;
1212 WRITELONG(p, 0); /* no symbol name */
1213 WRITELONG(p, (uint32_t) 0); /* offset zero */
1214 WRITELONG(p, (uint32_t) 0); /* size zero */
1215 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1216 WRITESHORT(p, debug_info); /* section id */
1217 saa_wbytes(s, entry, 16L);
1218 *len += 16;
1219 (*local)++;
1220 dwarf_abbrevsym = *local;
1221 p = entry;
1222 WRITELONG(p, 0); /* no symbol name */
1223 WRITELONG(p, (uint32_t) 0); /* offset zero */
1224 WRITELONG(p, (uint32_t) 0); /* size zero */
1225 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1226 WRITESHORT(p, debug_abbrev); /* section id */
1227 saa_wbytes(s, entry, 16L);
1228 *len += 16;
1229 (*local)++;
1230 dwarf_linesym = *local;
1231 p = entry;
1232 WRITELONG(p, 0); /* no symbol name */
1233 WRITELONG(p, (uint32_t) 0); /* offset zero */
1234 WRITELONG(p, (uint32_t) 0); /* size zero */
1235 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1236 WRITESHORT(p, debug_line); /* section id */
1237 saa_wbytes(s, entry, 16L);
1238 *len += 16;
1239 (*local)++;
1243 * Now the global symbols.
1245 saa_rewind(syms);
1246 while ((sym = saa_rstruct(syms))) {
1247 if (!(sym->type & SYM_GLOBAL))
1248 continue;
1249 p = entry;
1250 WRITELONG(p, sym->strpos);
1251 WRITELONG(p, sym->symv.key);
1252 WRITELONG(p, sym->size);
1253 WRITECHAR(p, sym->type); /* type and binding */
1254 WRITECHAR(p, sym->other); /* visibility */
1255 WRITESHORT(p, sym->section);
1256 saa_wbytes(s, entry, 16L);
1257 *len += 16;
1260 return s;
1263 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1265 struct SAA *s;
1266 uint8_t *p, entry[8];
1268 if (!r)
1269 return NULL;
1271 s = saa_init(1L);
1272 *len = 0;
1274 while (r) {
1275 int32_t sym = r->symbol;
1277 if (sym >= GLOBAL_TEMP_BASE)
1279 if (of_elf32.current_dfmt == &df_dwarf)
1280 sym += -GLOBAL_TEMP_BASE + (nsects + 5) + nlocals;
1281 else sym += -GLOBAL_TEMP_BASE + (nsects + 2) + nlocals;
1284 p = entry;
1285 WRITELONG(p, r->address);
1286 WRITELONG(p, (sym << 8) + r->type);
1287 saa_wbytes(s, entry, 8L);
1288 *len += 8;
1290 r = r->next;
1293 return s;
1296 static void elf_section_header(int name, int type, int flags,
1297 void *data, bool is_saa, int32_t datalen,
1298 int link, int info, int align, int eltsize)
1300 elf_sects[elf_nsect].data = data;
1301 elf_sects[elf_nsect].len = datalen;
1302 elf_sects[elf_nsect].is_saa = is_saa;
1303 elf_nsect++;
1305 fwriteint32_t((int32_t)name, elffp);
1306 fwriteint32_t((int32_t)type, elffp);
1307 fwriteint32_t((int32_t)flags, elffp);
1308 fwriteint32_t(0L, elffp); /* no address, ever, in object files */
1309 fwriteint32_t(type == 0 ? 0L : elf_foffs, elffp);
1310 fwriteint32_t(datalen, elffp);
1311 if (data)
1312 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1313 fwriteint32_t((int32_t)link, elffp);
1314 fwriteint32_t((int32_t)info, elffp);
1315 fwriteint32_t((int32_t)align, elffp);
1316 fwriteint32_t((int32_t)eltsize, elffp);
1319 static void elf_write_sections(void)
1321 int i;
1322 for (i = 0; i < elf_nsect; i++)
1323 if (elf_sects[i].data) {
1324 int32_t len = elf_sects[i].len;
1325 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1326 int32_t align = reallen - len;
1327 if (elf_sects[i].is_saa)
1328 saa_fpwrite(elf_sects[i].data, elffp);
1329 else
1330 fwrite(elf_sects[i].data, len, 1, elffp);
1331 fwritezero(align, elffp);
1335 static void elf_sect_write(struct Section *sect,
1336 const uint8_t *data, uint32_t len)
1338 saa_wbytes(sect->data, data, len);
1339 sect->len += len;
1342 static int32_t elf_segbase(int32_t segment)
1344 return segment;
1347 static int elf_directive(char *directive, char *value, int pass)
1349 bool err;
1350 int64_t n;
1351 char *p;
1353 if (!strcmp(directive, "osabi")) {
1354 if (pass == 2)
1355 return 1; /* ignore in pass 2 */
1357 n = readnum(value, &err);
1358 if (err) {
1359 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1360 return 1;
1362 if (n < 0 || n > 255) {
1363 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1364 return 1;
1366 elf_osabi = n;
1367 elf_abiver = 0;
1369 if ((p = strchr(value,',')) == NULL)
1370 return 1;
1372 n = readnum(p+1, &err);
1373 if (err || n < 0 || n > 255) {
1374 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1375 return 1;
1378 elf_abiver = n;
1379 return 1;
1382 return 0;
1385 static void elf_filename(char *inname, char *outname, efunc error)
1387 strcpy(elf_module, inname);
1388 standard_extension(inname, outname, ".o", error);
1391 extern macros_t elf_stdmac[];
1393 static int elf_set_info(enum geninfo type, char **val)
1395 (void)type;
1396 (void)val;
1397 return 0;
1399 static struct dfmt df_dwarf = {
1400 "ELF32 (i386) dwarf debug format for Linux/Unix",
1401 "dwarf",
1402 debug32_init,
1403 dwarf32_linenum,
1404 debug32_deflabel,
1405 debug32_directive,
1406 debug32_typevalue,
1407 dwarf32_output,
1408 dwarf32_cleanup
1410 static struct dfmt df_stabs = {
1411 "ELF32 (i386) stabs debug format for Linux/Unix",
1412 "stabs",
1413 debug32_init,
1414 stabs32_linenum,
1415 debug32_deflabel,
1416 debug32_directive,
1417 debug32_typevalue,
1418 stabs32_output,
1419 stabs32_cleanup
1422 struct dfmt *elf32_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1424 struct ofmt of_elf32 = {
1425 "ELF32 (i386) object files (e.g. Linux)",
1426 "elf32",
1427 NULL,
1428 elf32_debugs_arr,
1429 &df_stabs,
1430 elf_stdmac,
1431 elf_init,
1432 elf_set_info,
1433 elf_out,
1434 elf_deflabel,
1435 elf_section_names,
1436 elf_segbase,
1437 elf_directive,
1438 elf_filename,
1439 elf_cleanup
1442 struct ofmt of_elf = {
1443 "ELF (short name for ELF32) ",
1444 "elf",
1445 NULL,
1446 elf32_debugs_arr,
1447 &df_stabs,
1448 elf_stdmac,
1449 elf_init_hack,
1450 elf_set_info,
1451 elf_out,
1452 elf_deflabel,
1453 elf_section_names,
1454 elf_segbase,
1455 elf_directive,
1456 elf_filename,
1457 elf_cleanup
1459 /* again, the stabs debugging stuff (code) */
1461 void debug32_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1463 (void)of;
1464 (void)id;
1465 (void)fp;
1466 (void)error;
1469 void stabs32_linenum(const char *filename, int32_t linenumber, int32_t segto)
1471 (void)segto;
1473 if (!stabs_filename) {
1474 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1475 strcpy(stabs_filename, filename);
1476 } else {
1477 if (strcmp(stabs_filename, filename)) {
1478 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1479 in fact, this leak comes in quite handy to maintain a list of files
1480 encountered so far in the symbol lines... */
1482 /* why not nasm_free(stabs_filename); we're done with the old one */
1484 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1485 strcpy(stabs_filename, filename);
1488 debug_immcall = 1;
1489 currentline = linenumber;
1492 void debug32_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1493 char *special)
1495 (void)name;
1496 (void)segment;
1497 (void)offset;
1498 (void)is_global;
1499 (void)special;
1502 void debug32_directive(const char *directive, const char *params)
1504 (void)directive;
1505 (void)params;
1508 void debug32_typevalue(int32_t type)
1510 int32_t stype, ssize;
1511 switch (TYM_TYPE(type)) {
1512 case TY_LABEL:
1513 ssize = 0;
1514 stype = STT_NOTYPE;
1515 break;
1516 case TY_BYTE:
1517 ssize = 1;
1518 stype = STT_OBJECT;
1519 break;
1520 case TY_WORD:
1521 ssize = 2;
1522 stype = STT_OBJECT;
1523 break;
1524 case TY_DWORD:
1525 ssize = 4;
1526 stype = STT_OBJECT;
1527 break;
1528 case TY_FLOAT:
1529 ssize = 4;
1530 stype = STT_OBJECT;
1531 break;
1532 case TY_QWORD:
1533 ssize = 8;
1534 stype = STT_OBJECT;
1535 break;
1536 case TY_TBYTE:
1537 ssize = 10;
1538 stype = STT_OBJECT;
1539 break;
1540 case TY_OWORD:
1541 ssize = 8;
1542 stype = STT_OBJECT;
1543 break;
1544 case TY_COMMON:
1545 ssize = 0;
1546 stype = STT_COMMON;
1547 break;
1548 case TY_SEG:
1549 ssize = 0;
1550 stype = STT_SECTION;
1551 break;
1552 case TY_EXTERN:
1553 ssize = 0;
1554 stype = STT_NOTYPE;
1555 break;
1556 case TY_EQU:
1557 ssize = 0;
1558 stype = STT_NOTYPE;
1559 break;
1560 default:
1561 ssize = 0;
1562 stype = STT_NOTYPE;
1563 break;
1565 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1566 lastsym->size = ssize;
1567 lastsym->type = stype;
1571 void stabs32_output(int type, void *param)
1573 struct symlininfo *s;
1574 struct linelist *el;
1575 if (type == TY_STABSSYMLIN) {
1576 if (debug_immcall) {
1577 s = (struct symlininfo *)param;
1578 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1579 return; /* we are only interested in the text stuff */
1580 numlinestabs++;
1581 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1582 el->info.offset = s->offset;
1583 el->info.section = s->section;
1584 el->info.name = s->name;
1585 el->line = currentline;
1586 el->filename = stabs_filename;
1587 el->next = 0;
1588 if (stabslines) {
1589 stabslines->last->next = el;
1590 stabslines->last = el;
1591 } else {
1592 stabslines = el;
1593 stabslines->last = el;
1597 debug_immcall = 0;
1600 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1601 do {\
1602 WRITELONG(p,n_strx); \
1603 WRITECHAR(p,n_type); \
1604 WRITECHAR(p,n_other); \
1605 WRITESHORT(p,n_desc); \
1606 WRITELONG(p,n_value); \
1607 } while (0)
1609 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1611 void stabs32_generate(void)
1613 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1614 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1615 char **allfiles;
1616 int *fileidx;
1618 struct linelist *ptr;
1620 ptr = stabslines;
1622 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1623 for (i = 0; i < numlinestabs; i++)
1624 allfiles[i] = 0;
1625 numfiles = 0;
1626 while (ptr) {
1627 if (numfiles == 0) {
1628 allfiles[0] = ptr->filename;
1629 numfiles++;
1630 } else {
1631 for (i = 0; i < numfiles; i++) {
1632 if (!strcmp(allfiles[i], ptr->filename))
1633 break;
1635 if (i >= numfiles) {
1636 allfiles[i] = ptr->filename;
1637 numfiles++;
1640 ptr = ptr->next;
1642 strsize = 1;
1643 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1644 for (i = 0; i < numfiles; i++) {
1645 fileidx[i] = strsize;
1646 strsize += strlen(allfiles[i]) + 1;
1648 mainfileindex = 0;
1649 for (i = 0; i < numfiles; i++) {
1650 if (!strcmp(allfiles[i], elf_module)) {
1651 mainfileindex = i;
1652 break;
1656 /* worst case size of the stab buffer would be:
1657 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1659 sbuf =
1660 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1661 sizeof(struct stabentry));
1663 ssbuf = (uint8_t *)nasm_malloc(strsize);
1665 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1666 rptr = rbuf;
1668 for (i = 0; i < numfiles; i++) {
1669 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1671 ssbuf[0] = 0;
1673 stabstrlen = strsize; /* set global variable for length of stab strings */
1675 sptr = sbuf;
1676 ptr = stabslines;
1677 numstabs = 0;
1679 if (ptr) {
1680 /* this is the first stab, its strx points to the filename of the
1681 the source-file, the n_desc field should be set to the number
1682 of remaining stabs
1684 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1686 /* this is the stab for the main source file */
1687 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1689 /* relocation table entry */
1691 /* Since the symbol table has two entries before */
1692 /* the section symbols, the index in the info.section */
1693 /* member must be adjusted by adding 2 */
1695 WRITELONG(rptr, (sptr - sbuf) - 4);
1696 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1698 numstabs++;
1699 currfile = mainfileindex;
1702 while (ptr) {
1703 if (strcmp(allfiles[currfile], ptr->filename)) {
1704 /* oops file has changed... */
1705 for (i = 0; i < numfiles; i++)
1706 if (!strcmp(allfiles[i], ptr->filename))
1707 break;
1708 currfile = i;
1709 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1710 ptr->info.offset);
1711 numstabs++;
1713 /* relocation table entry */
1714 WRITELONG(rptr, (sptr - sbuf) - 4);
1715 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1718 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1719 numstabs++;
1721 /* relocation table entry */
1723 WRITELONG(rptr, (sptr - sbuf) - 4);
1724 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1726 ptr = ptr->next;
1730 ((struct stabentry *)sbuf)->n_desc = numstabs;
1732 nasm_free(allfiles);
1733 nasm_free(fileidx);
1735 stablen = (sptr - sbuf);
1736 stabrellen = (rptr - rbuf);
1737 stabrelbuf = rbuf;
1738 stabbuf = sbuf;
1739 stabstrbuf = ssbuf;
1742 void stabs32_cleanup(void)
1744 struct linelist *ptr, *del;
1745 if (!stabslines)
1746 return;
1747 ptr = stabslines;
1748 while (ptr) {
1749 del = ptr;
1750 ptr = ptr->next;
1751 nasm_free(del);
1753 if (stabbuf)
1754 nasm_free(stabbuf);
1755 if (stabrelbuf)
1756 nasm_free(stabrelbuf);
1757 if (stabstrbuf)
1758 nasm_free(stabstrbuf);
1760 /* dwarf routines */
1763 void dwarf32_linenum(const char *filename, int32_t linenumber, int32_t segto)
1765 (void)segto;
1766 dwarf32_findfile(filename);
1767 debug_immcall = 1;
1768 currentline = linenumber;
1771 /* called from elf_out with type == TY_DEBUGSYMLIN */
1772 void dwarf32_output(int type, void *param)
1774 int ln, aa, inx, maxln, soc;
1775 struct symlininfo *s;
1776 struct SAA *plinep;
1778 (void)type;
1780 s = (struct symlininfo *)param;
1781 /* line number info is only gathered for executable sections */
1782 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1783 return;
1784 /* Check if section index has changed */
1785 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1787 dwarf32_findsect(s->section);
1789 /* do nothing unless line or file has changed */
1790 if (debug_immcall)
1792 ln = currentline - dwarf_csect->line;
1793 aa = s->offset - dwarf_csect->offset;
1794 inx = dwarf_clist->line;
1795 plinep = dwarf_csect->psaa;
1796 /* check for file change */
1797 if (!(inx == dwarf_csect->file))
1799 saa_write8(plinep,DW_LNS_set_file);
1800 saa_write8(plinep,inx);
1801 dwarf_csect->file = inx;
1803 /* check for line change */
1804 if (ln)
1806 /* test if in range of special op code */
1807 maxln = line_base + line_range;
1808 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1809 if (ln >= line_base && ln < maxln && soc < 256)
1811 saa_write8(plinep,soc);
1813 else
1815 if (ln)
1817 saa_write8(plinep,DW_LNS_advance_line);
1818 saa_wleb128s(plinep,ln);
1820 if (aa)
1822 saa_write8(plinep,DW_LNS_advance_pc);
1823 saa_wleb128u(plinep,aa);
1826 dwarf_csect->line = currentline;
1827 dwarf_csect->offset = s->offset;
1829 /* show change handled */
1830 debug_immcall = 0;
1835 void dwarf32_generate(void)
1837 uint8_t *pbuf;
1838 int indx;
1839 struct linelist *ftentry;
1840 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1841 struct SAA *parangesrel, *plinesrel, *pinforel;
1842 struct sectlist *psect;
1843 size_t saalen, linepoff, totlen, highaddr;
1845 /* write epilogues for each line program range */
1846 /* and build aranges section */
1847 paranges = saa_init(1L);
1848 parangesrel = saa_init(1L);
1849 saa_write16(paranges,2); /* dwarf version */
1850 saa_write32(parangesrel, paranges->datalen+4);
1851 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
1852 saa_write32(parangesrel, 0);
1853 saa_write32(paranges,0); /* offset into info */
1854 saa_write8(paranges,4); /* pointer size */
1855 saa_write8(paranges,0); /* not segmented */
1856 saa_write32(paranges,0); /* padding */
1857 /* iterate though sectlist entries */
1858 psect = dwarf_fsect;
1859 totlen = 0;
1860 highaddr = 0;
1861 for (indx = 0; indx < dwarf_nsections; indx++)
1863 plinep = psect->psaa;
1864 /* Line Number Program Epilogue */
1865 saa_write8(plinep,2); /* std op 2 */
1866 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1867 saa_write8(plinep,DW_LNS_extended_op);
1868 saa_write8(plinep,1); /* operand length */
1869 saa_write8(plinep,DW_LNE_end_sequence);
1870 totlen += plinep->datalen;
1871 /* range table relocation entry */
1872 saa_write32(parangesrel, paranges->datalen + 4);
1873 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
1874 saa_write32(parangesrel, (uint32_t) 0);
1875 /* range table entry */
1876 saa_write32(paranges,0x0000); /* range start */
1877 saa_write32(paranges,sects[psect->section]->len); /* range length */
1878 highaddr += sects[psect->section]->len;
1879 /* done with this entry */
1880 psect = psect->next;
1882 saa_write32(paranges,0); /* null address */
1883 saa_write32(paranges,0); /* null length */
1884 saalen = paranges->datalen;
1885 arangeslen = saalen + 4;
1886 arangesbuf = pbuf = nasm_malloc(arangeslen);
1887 WRITELONG(pbuf,saalen); /* initial length */
1888 saa_rnbytes(paranges, pbuf, saalen);
1889 saa_free(paranges);
1891 /* build rela.aranges section */
1892 arangesrellen = saalen = parangesrel->datalen;
1893 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1894 saa_rnbytes(parangesrel, pbuf, saalen);
1895 saa_free(parangesrel);
1897 /* build pubnames section */
1898 ppubnames = saa_init(1L);
1899 saa_write16(ppubnames,3); /* dwarf version */
1900 saa_write32(ppubnames,0); /* offset into info */
1901 saa_write32(ppubnames,0); /* space used in info */
1902 saa_write32(ppubnames,0); /* end of list */
1903 saalen = ppubnames->datalen;
1904 pubnameslen = saalen + 4;
1905 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1906 WRITELONG(pbuf,saalen); /* initial length */
1907 saa_rnbytes(ppubnames, pbuf, saalen);
1908 saa_free(ppubnames);
1910 /* build info section */
1911 pinfo = saa_init(1L);
1912 pinforel = saa_init(1L);
1913 saa_write16(pinfo,2); /* dwarf version */
1914 saa_write32(pinforel, pinfo->datalen + 4);
1915 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
1916 saa_write32(pinforel, 0);
1917 saa_write32(pinfo,0); /* offset into abbrev */
1918 saa_write8(pinfo,4); /* pointer size */
1919 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1920 saa_write32(pinforel, pinfo->datalen + 4);
1921 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1922 saa_write32(pinforel, 0);
1923 saa_write32(pinfo,0); /* DW_AT_low_pc */
1924 saa_write32(pinforel, pinfo->datalen + 4);
1925 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1926 saa_write32(pinforel, 0);
1927 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
1928 saa_write32(pinforel, pinfo->datalen + 4);
1929 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
1930 saa_write32(pinforel, 0);
1931 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1932 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1933 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1934 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1935 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1936 saa_write32(pinforel, pinfo->datalen + 4);
1937 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1938 saa_write32(pinforel, 0);
1939 saa_write32(pinfo,0); /* DW_AT_low_pc */
1940 saa_write32(pinfo,0); /* DW_AT_frame_base */
1941 saa_write8(pinfo,0); /* end of entries */
1942 saalen = pinfo->datalen;
1943 infolen = saalen + 4;
1944 infobuf = pbuf = nasm_malloc(infolen);
1945 WRITELONG(pbuf,saalen); /* initial length */
1946 saa_rnbytes(pinfo, pbuf, saalen);
1947 saa_free(pinfo);
1949 /* build rela.info section */
1950 inforellen = saalen = pinforel->datalen;
1951 inforelbuf = pbuf = nasm_malloc(inforellen);
1952 saa_rnbytes(pinforel, pbuf, saalen);
1953 saa_free(pinforel);
1955 /* build abbrev section */
1956 pabbrev = saa_init(1L);
1957 saa_write8(pabbrev,1); /* entry number LEB128u */
1958 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
1959 saa_write8(pabbrev,1); /* has children */
1960 /* the following attributes and forms are all LEB128u values */
1961 saa_write8(pabbrev,DW_AT_low_pc);
1962 saa_write8(pabbrev,DW_FORM_addr);
1963 saa_write8(pabbrev,DW_AT_high_pc);
1964 saa_write8(pabbrev,DW_FORM_addr);
1965 saa_write8(pabbrev,DW_AT_stmt_list);
1966 saa_write8(pabbrev,DW_FORM_data4);
1967 saa_write8(pabbrev,DW_AT_name);
1968 saa_write8(pabbrev,DW_FORM_string);
1969 saa_write8(pabbrev,DW_AT_producer);
1970 saa_write8(pabbrev,DW_FORM_string);
1971 saa_write8(pabbrev,DW_AT_language);
1972 saa_write8(pabbrev,DW_FORM_data2);
1973 saa_write16(pabbrev,0); /* end of entry */
1974 /* LEB128u usage same as above */
1975 saa_write8(pabbrev,2); /* entry number */
1976 saa_write8(pabbrev,DW_TAG_subprogram);
1977 saa_write8(pabbrev,0); /* no children */
1978 saa_write8(pabbrev,DW_AT_low_pc);
1979 saa_write8(pabbrev,DW_FORM_addr);
1980 saa_write8(pabbrev,DW_AT_frame_base);
1981 saa_write8(pabbrev,DW_FORM_data4);
1982 saa_write16(pabbrev,0); /* end of entry */
1983 abbrevlen = saalen = pabbrev->datalen;
1984 abbrevbuf = pbuf = nasm_malloc(saalen);
1985 saa_rnbytes(pabbrev, pbuf, saalen);
1986 saa_free(pabbrev);
1988 /* build line section */
1989 /* prolog */
1990 plines = saa_init(1L);
1991 saa_write8(plines,1); /* Minimum Instruction Length */
1992 saa_write8(plines,1); /* Initial value of 'is_stmt' */
1993 saa_write8(plines,line_base); /* Line Base */
1994 saa_write8(plines,line_range); /* Line Range */
1995 saa_write8(plines,opcode_base); /* Opcode Base */
1996 /* standard opcode lengths (# of LEB128u operands) */
1997 saa_write8(plines,0); /* Std opcode 1 length */
1998 saa_write8(plines,1); /* Std opcode 2 length */
1999 saa_write8(plines,1); /* Std opcode 3 length */
2000 saa_write8(plines,1); /* Std opcode 4 length */
2001 saa_write8(plines,1); /* Std opcode 5 length */
2002 saa_write8(plines,0); /* Std opcode 6 length */
2003 saa_write8(plines,0); /* Std opcode 7 length */
2004 saa_write8(plines,0); /* Std opcode 8 length */
2005 saa_write8(plines,1); /* Std opcode 9 length */
2006 saa_write8(plines,0); /* Std opcode 10 length */
2007 saa_write8(plines,0); /* Std opcode 11 length */
2008 saa_write8(plines,1); /* Std opcode 12 length */
2009 /* Directory Table */
2010 saa_write8(plines,0); /* End of table */
2011 /* File Name Table */
2012 ftentry = dwarf_flist;
2013 for (indx = 0;indx<dwarf_numfiles;indx++)
2015 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2016 saa_write8(plines,0); /* directory LEB128u */
2017 saa_write8(plines,0); /* time LEB128u */
2018 saa_write8(plines,0); /* size LEB128u */
2019 ftentry = ftentry->next;
2021 saa_write8(plines,0); /* End of table */
2022 linepoff = plines->datalen;
2023 linelen = linepoff + totlen + 10;
2024 linebuf = pbuf = nasm_malloc(linelen);
2025 WRITELONG(pbuf,linelen-4); /* initial length */
2026 WRITESHORT(pbuf,3); /* dwarf version */
2027 WRITELONG(pbuf,linepoff); /* offset to line number program */
2028 /* write line header */
2029 saalen = linepoff;
2030 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2031 pbuf += linepoff;
2032 saa_free(plines);
2033 /* concatonate line program ranges */
2034 linepoff += 13;
2035 plinesrel = saa_init(1L);
2036 psect = dwarf_fsect;
2037 for (indx = 0; indx < dwarf_nsections; indx++)
2039 saa_write32(plinesrel, linepoff);
2040 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
2041 saa_write32(plinesrel, (uint32_t) 0);
2042 plinep = psect->psaa;
2043 saalen = plinep->datalen;
2044 saa_rnbytes(plinep, pbuf, saalen);
2045 pbuf += saalen;
2046 linepoff += saalen;
2047 saa_free(plinep);
2048 /* done with this entry */
2049 psect = psect->next;
2053 /* build rela.lines section */
2054 linerellen =saalen = plinesrel->datalen;
2055 linerelbuf = pbuf = nasm_malloc(linerellen);
2056 saa_rnbytes(plinesrel, pbuf, saalen);
2057 saa_free(plinesrel);
2059 /* build frame section */
2060 framelen = 4;
2061 framebuf = pbuf = nasm_malloc(framelen);
2062 WRITELONG(pbuf,framelen-4); /* initial length */
2064 /* build loc section */
2065 loclen = 16;
2066 locbuf = pbuf = nasm_malloc(loclen);
2067 WRITELONG(pbuf,0); /* null beginning offset */
2068 WRITELONG(pbuf,0); /* null ending offset */
2071 void dwarf32_cleanup(void)
2073 if (arangesbuf)
2074 nasm_free(arangesbuf);
2075 if (arangesrelbuf)
2076 nasm_free(arangesrelbuf);
2077 if (pubnamesbuf)
2078 nasm_free(pubnamesbuf);
2079 if (infobuf)
2080 nasm_free(infobuf);
2081 if (inforelbuf)
2082 nasm_free(inforelbuf);
2083 if (abbrevbuf)
2084 nasm_free(abbrevbuf);
2085 if (linebuf)
2086 nasm_free(linebuf);
2087 if (linerelbuf)
2088 nasm_free(linerelbuf);
2089 if (framebuf)
2090 nasm_free(framebuf);
2091 if (locbuf)
2092 nasm_free(locbuf);
2094 void dwarf32_findfile(const char * fname)
2096 int finx;
2097 struct linelist *match;
2099 /* return if fname is current file name */
2100 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2101 /* search for match */
2102 else
2104 match = 0;
2105 if (dwarf_flist)
2107 match = dwarf_flist;
2108 for (finx = 0; finx < dwarf_numfiles; finx++)
2110 if (!(strcmp(fname, match->filename)))
2112 dwarf_clist = match;
2113 return;
2117 /* add file name to end of list */
2118 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2119 dwarf_numfiles++;
2120 dwarf_clist->line = dwarf_numfiles;
2121 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2122 strcpy(dwarf_clist->filename,fname);
2123 dwarf_clist->next = 0;
2124 /* if first entry */
2125 if (!dwarf_flist)
2127 dwarf_flist = dwarf_elist = dwarf_clist;
2128 dwarf_clist->last = 0;
2130 /* chain to previous entry */
2131 else
2133 dwarf_elist->next = dwarf_clist;
2134 dwarf_elist = dwarf_clist;
2138 /* */
2139 void dwarf32_findsect(const int index)
2141 int sinx;
2142 struct sectlist *match;
2143 struct SAA *plinep;
2144 /* return if index is current section index */
2145 if (dwarf_csect && (dwarf_csect->section == index))
2147 return;
2149 /* search for match */
2150 else
2152 match = 0;
2153 if (dwarf_fsect)
2155 match = dwarf_fsect;
2156 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2158 if ((match->section == index))
2160 dwarf_csect = match;
2161 return;
2163 match = match->next;
2166 /* add entry to end of list */
2167 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2168 dwarf_nsections++;
2169 dwarf_csect->psaa = plinep = saa_init(1L);
2170 dwarf_csect->line = 1;
2171 dwarf_csect->offset = 0;
2172 dwarf_csect->file = 1;
2173 dwarf_csect->section = index;
2174 dwarf_csect->next = 0;
2175 /* set relocatable address at start of line program */
2176 saa_write8(plinep,DW_LNS_extended_op);
2177 saa_write8(plinep,5); /* operand length */
2178 saa_write8(plinep,DW_LNE_set_address);
2179 saa_write32(plinep,0); /* Start Address */
2180 /* if first entry */
2181 if (!dwarf_fsect)
2183 dwarf_fsect = dwarf_esect = dwarf_csect;
2184 dwarf_csect->last = 0;
2186 /* chain to previous entry */
2187 else
2189 dwarf_esect->next = dwarf_csect;
2190 dwarf_esect = dwarf_csect;
2195 #endif /* OF_ELF */