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