Remove function pointers in output, simplify error handling
[nasm/sigaren-mirror.git] / output / outelf32.c
blobcec3feb67e1ca1467ef6974f4882bf1c78f6f17c
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 "eval.h"
53 #include "output/outform.h"
54 #include "output/outlib.h"
55 #include "rbtree.h"
57 #include "output/elf32.h"
58 #include "output/dwarf.h"
59 #include "output/outelf.h"
61 #ifdef OF_ELF32
64 * Relocation types.
66 struct Reloc {
67 struct Reloc *next;
68 int32_t address; /* relative to _start_ of section */
69 int32_t symbol; /* symbol index */
70 int type; /* type of relocation */
73 struct Symbol {
74 struct rbtree symv; /* symbol value and symbol rbtree */
75 int32_t strpos; /* string table position of name */
76 int32_t section; /* section ID of the symbol */
77 int type; /* symbol type */
78 int other; /* symbol visibility */
79 int32_t size; /* size of symbol */
80 int32_t globnum; /* symbol table offset if global */
81 struct Symbol *nextfwd; /* list of unresolved-size symbols */
82 char *name; /* used temporarily if in above list */
85 struct Section {
86 struct SAA *data;
87 uint32_t len, size, nrelocs;
88 int32_t index;
89 int type; /* SHT_PROGBITS or SHT_NOBITS */
90 uint32_t align; /* alignment: power of two */
91 uint32_t flags; /* section flags */
92 char *name;
93 struct SAA *rel;
94 int32_t rellen;
95 struct Reloc *head, **tail;
96 struct rbtree *gsyms; /* global symbols in section */
99 #define SECT_DELTA 32
100 static struct Section **sects;
101 static int nsects, sectlen;
103 #define SHSTR_DELTA 256
104 static char *shstrtab;
105 static int shstrtablen, shstrtabsize;
107 static struct SAA *syms;
108 static uint32_t nlocals, nglobs, ndebugs; /* Symbol counts */
110 static int32_t def_seg;
112 static struct RAA *bsym;
114 static struct SAA *strs;
115 static uint32_t strslen;
117 static struct Symbol *fwds;
119 static char elf_module[FILENAME_MAX];
121 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
122 static uint8_t elf_abiver = 0; /* Current ABI version */
124 extern struct ofmt of_elf32;
125 extern struct ofmt of_elf;
127 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
129 static struct ELF_SECTDATA {
130 void *data;
131 int32_t len;
132 bool is_saa;
133 } *elf_sects;
134 static int elf_nsect, nsections;
135 static int32_t elf_foffs;
137 static void elf_write(void);
138 static void elf_sect_write(struct Section *, const uint8_t *,
139 uint32_t);
140 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
141 int, int);
142 static void elf_write_sections(void);
143 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
144 static struct SAA *elf_build_reltab(int32_t *, struct Reloc *);
145 static void add_sectname(char *, char *);
147 struct stabentry {
148 uint32_t n_strx;
149 uint8_t n_type;
150 uint8_t n_other;
151 uint16_t n_desc;
152 uint32_t n_value;
155 struct erel {
156 int offset, info;
159 struct symlininfo {
160 int offset;
161 int section; /* section index */
162 char *name; /* shallow-copied pointer of section name */
165 struct linelist {
166 struct symlininfo info;
167 int line;
168 char *filename;
169 struct linelist *next;
170 struct linelist *last;
173 struct sectlist {
174 struct SAA *psaa;
175 int section;
176 int line;
177 int offset;
178 int file;
179 struct sectlist *next;
180 struct sectlist *last;
183 /* common debug variables */
184 static int currentline = 1;
185 static int debug_immcall = 0;
187 /* stabs debug variables */
188 static struct linelist *stabslines = 0;
189 static int numlinestabs = 0;
190 static char *stabs_filename = 0;
191 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
192 static int stablen, stabstrlen, stabrellen;
194 /* dwarf debug variables */
195 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
196 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
197 static int dwarf_numfiles = 0, dwarf_nsections;
198 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
199 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
200 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
201 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
202 abbrevlen, linelen, linerellen, framelen, loclen;
203 static int32_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
205 static struct dfmt df_dwarf;
206 static struct dfmt df_stabs;
207 static struct Symbol *lastsym;
209 /* common debugging routines */
210 static void debug32_typevalue(int32_t);
211 static void debug32_deflabel(char *, int32_t, int64_t, int, char *);
212 static void debug32_directive(const char *, const char *);
214 /* stabs debugging routines */
215 static void stabs32_linenum(const char *filename, int32_t linenumber, int32_t);
216 static void stabs32_output(int, void *);
217 static void stabs32_generate(void);
218 static void stabs32_cleanup(void);
220 /* dwarf debugging routines */
221 static void dwarf32_init(void);
222 static void dwarf32_linenum(const char *filename, int32_t linenumber, int32_t);
223 static void dwarf32_output(int, void *);
224 static void dwarf32_generate(void);
225 static void dwarf32_cleanup(void);
226 static void dwarf32_findfile(const char *);
227 static void dwarf32_findsect(const int);
230 * Special NASM section numbers which are used to define ELF special
231 * symbols, which can be used with WRT to provide PIC and TLS
232 * relocation types.
234 static int32_t elf_gotpc_sect, elf_gotoff_sect;
235 static int32_t elf_got_sect, elf_plt_sect;
236 static int32_t elf_sym_sect, elf_tlsie_sect;
238 static void elf_init(void)
240 sects = NULL;
241 nsects = sectlen = 0;
242 syms = saa_init((int32_t)sizeof(struct Symbol));
243 nlocals = nglobs = ndebugs = 0;
244 bsym = raa_init();
245 strs = saa_init(1L);
246 saa_wbytes(strs, "\0", 1L);
247 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
248 strslen = 2 + strlen(elf_module);
249 shstrtab = NULL;
250 shstrtablen = shstrtabsize = 0;;
251 add_sectname("", "");
253 fwds = NULL;
255 elf_gotpc_sect = seg_alloc();
256 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf32,
257 nasm_error);
258 elf_gotoff_sect = seg_alloc();
259 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf32,
260 nasm_error);
261 elf_got_sect = seg_alloc();
262 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf32,
263 nasm_error);
264 elf_plt_sect = seg_alloc();
265 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf32,
266 nasm_error);
267 elf_sym_sect = seg_alloc();
268 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf32,
269 nasm_error);
270 elf_tlsie_sect = seg_alloc();
271 define_label("..tlsie", elf_tlsie_sect + 1, 0L, NULL, false, false, &of_elf32,
272 nasm_error);
274 def_seg = seg_alloc();
277 static void elf_init_hack(void)
279 of_elf32.current_dfmt = of_elf.current_dfmt; /* Sync debugging format */
280 elf_init();
283 static void elf_cleanup(int debuginfo)
285 struct Reloc *r;
286 int i;
288 (void)debuginfo;
290 elf_write();
291 for (i = 0; i < nsects; i++) {
292 if (sects[i]->type != SHT_NOBITS)
293 saa_free(sects[i]->data);
294 if (sects[i]->head)
295 saa_free(sects[i]->rel);
296 while (sects[i]->head) {
297 r = sects[i]->head;
298 sects[i]->head = sects[i]->head->next;
299 nasm_free(r);
302 nasm_free(sects);
303 saa_free(syms);
304 raa_free(bsym);
305 saa_free(strs);
306 if (of_elf32.current_dfmt) {
307 of_elf32.current_dfmt->cleanup();
311 static void add_sectname(char *firsthalf, char *secondhalf)
313 int len = strlen(firsthalf) + strlen(secondhalf);
314 while (shstrtablen + len + 1 > shstrtabsize)
315 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
316 strcpy(shstrtab + shstrtablen, firsthalf);
317 strcat(shstrtab + shstrtablen, secondhalf);
318 shstrtablen += len + 1;
321 static int elf_make_section(char *name, int type, int flags, int align)
323 struct Section *s;
325 s = nasm_malloc(sizeof(*s));
327 if (type != SHT_NOBITS)
328 s->data = saa_init(1L);
329 s->head = NULL;
330 s->tail = &s->head;
331 s->len = s->size = 0;
332 s->nrelocs = 0;
333 if (!strcmp(name, ".text"))
334 s->index = def_seg;
335 else
336 s->index = seg_alloc();
337 add_sectname("", name);
338 s->name = nasm_malloc(1 + strlen(name));
339 strcpy(s->name, name);
340 s->type = type;
341 s->flags = flags;
342 s->align = align;
343 s->gsyms = NULL;
345 if (nsects >= sectlen)
346 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
347 sects[nsects++] = s;
349 return nsects - 1;
353 static int32_t elf_section_names(char *name, int pass, int *bits)
355 char *p;
356 uint32_t flags, flags_and, flags_or;
357 uint32_t align;
358 int type, i;
361 * Default is 32 bits.
363 if (!name) {
364 *bits = 32;
365 return def_seg;
368 p = name;
369 while (*p && !nasm_isspace(*p))
370 p++;
371 if (*p)
372 *p++ = '\0';
373 flags_and = flags_or = type = align = 0;
375 while (*p && nasm_isspace(*p))
376 p++;
377 while (*p) {
378 char *q = p;
379 while (*p && !nasm_isspace(*p))
380 p++;
381 if (*p)
382 *p++ = '\0';
383 while (*p && nasm_isspace(*p))
384 p++;
386 if (!nasm_strnicmp(q, "align=", 6)) {
387 align = atoi(q + 6);
388 if (align == 0)
389 align = 1;
390 if ((align - 1) & align) { /* means it's not a power of two */
391 nasm_error(ERR_NONFATAL, "section alignment %d is not"
392 " a power of two", align);
393 align = 1;
395 } else if (!nasm_stricmp(q, "alloc")) {
396 flags_and |= SHF_ALLOC;
397 flags_or |= SHF_ALLOC;
398 } else if (!nasm_stricmp(q, "noalloc")) {
399 flags_and |= SHF_ALLOC;
400 flags_or &= ~SHF_ALLOC;
401 } else if (!nasm_stricmp(q, "exec")) {
402 flags_and |= SHF_EXECINSTR;
403 flags_or |= SHF_EXECINSTR;
404 } else if (!nasm_stricmp(q, "noexec")) {
405 flags_and |= SHF_EXECINSTR;
406 flags_or &= ~SHF_EXECINSTR;
407 } else if (!nasm_stricmp(q, "write")) {
408 flags_and |= SHF_WRITE;
409 flags_or |= SHF_WRITE;
410 } else if (!nasm_stricmp(q, "tls")) {
411 flags_and |= SHF_TLS;
412 flags_or |= SHF_TLS;
413 } else if (!nasm_stricmp(q, "nowrite")) {
414 flags_and |= SHF_WRITE;
415 flags_or &= ~SHF_WRITE;
416 } else if (!nasm_stricmp(q, "progbits")) {
417 type = SHT_PROGBITS;
418 } else if (!nasm_stricmp(q, "nobits")) {
419 type = SHT_NOBITS;
420 } else if (pass == 1) {
421 nasm_error(ERR_WARNING, "Unknown section attribute '%s' ignored on"
422 " declaration of section `%s'", q, name);
426 if (!strcmp(name, ".shstrtab") ||
427 !strcmp(name, ".symtab") ||
428 !strcmp(name, ".strtab")) {
429 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
430 "name `%s'", name);
431 return NO_SEG;
434 for (i = 0; i < nsects; i++)
435 if (!strcmp(name, sects[i]->name))
436 break;
437 if (i == nsects) {
438 const struct elf_known_section *ks = elf_known_sections;
440 while (ks->name) {
441 if (!strcmp(name, ks->name))
442 break;
443 ks++;
446 type = type ? type : ks->type;
447 align = align ? align : ks->align;
448 flags = (ks->flags & ~flags_and) | flags_or;
450 i = elf_make_section(name, type, flags, align);
451 } else if (pass == 1) {
452 if ((type && sects[i]->type != type)
453 || (align && sects[i]->align != align)
454 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
455 nasm_error(ERR_WARNING, "section attributes ignored on"
456 " redeclaration of section `%s'", name);
459 return sects[i]->index;
462 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
463 int is_global, char *special)
465 int pos = strslen;
466 struct Symbol *sym;
467 bool special_used = false;
469 #if defined(DEBUG) && DEBUG>2
470 fprintf(stderr,
471 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
472 name, segment, offset, is_global, special);
473 #endif
474 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
476 * This is a NASM special symbol. We never allow it into
477 * the ELF symbol table, even if it's a valid one. If it
478 * _isn't_ a valid one, we should barf immediately.
480 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
481 strcmp(name, "..got") && strcmp(name, "..plt") &&
482 strcmp(name, "..sym") && strcmp(name, "..tlsie"))
483 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
484 return;
487 if (is_global == 3) {
488 struct Symbol **s;
490 * Fix up a forward-reference symbol size from the first
491 * pass.
493 for (s = &fwds; *s; s = &(*s)->nextfwd)
494 if (!strcmp((*s)->name, name)) {
495 struct tokenval tokval;
496 expr *e;
497 char *p = special;
499 while (*p && !nasm_isspace(*p))
500 p++;
501 while (*p && nasm_isspace(*p))
502 p++;
503 stdscan_reset();
504 stdscan_bufptr = p;
505 tokval.t_type = TOKEN_INVALID;
506 e = evaluate(stdscan, NULL, &tokval, NULL, 1, nasm_error, NULL);
507 if (e) {
508 if (!is_simple(e))
509 nasm_error(ERR_NONFATAL, "cannot use relocatable"
510 " expression as symbol size");
511 else
512 (*s)->size = reloc_value(e);
516 * Remove it from the list of unresolved sizes.
518 nasm_free((*s)->name);
519 *s = (*s)->nextfwd;
520 return;
522 return; /* it wasn't an important one */
525 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
526 strslen += 1 + strlen(name);
528 lastsym = sym = saa_wstruct(syms);
530 memset(&sym->symv, 0, sizeof(struct rbtree));
532 sym->strpos = pos;
533 sym->type = is_global ? SYM_GLOBAL : 0;
534 sym->other = STV_DEFAULT;
535 sym->size = 0;
536 if (segment == NO_SEG)
537 sym->section = SHN_ABS;
538 else {
539 int i;
540 sym->section = SHN_UNDEF;
541 if (nsects == 0 && segment == def_seg) {
542 int tempint;
543 if (segment != elf_section_names(".text", 2, &tempint))
544 nasm_error(ERR_PANIC,
545 "strange segment conditions in ELF driver");
546 sym->section = nsects;
547 } else {
548 for (i = 0; i < nsects; i++)
549 if (segment == sects[i]->index) {
550 sym->section = i + 1;
551 break;
556 if (is_global == 2) {
557 sym->size = offset;
558 sym->symv.key = 0;
559 sym->section = SHN_COMMON;
561 * We have a common variable. Check the special text to see
562 * if it's a valid number and power of two; if so, store it
563 * as the alignment for the common variable.
565 if (special) {
566 bool err;
567 sym->symv.key = readnum(special, &err);
568 if (err)
569 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
570 " valid number", special);
571 else if ((sym->symv.key | (sym->symv.key - 1))
572 != 2 * sym->symv.key - 1)
573 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
574 " power of two", special);
576 special_used = true;
577 } else
578 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
580 if (sym->type == SYM_GLOBAL) {
582 * If sym->section == SHN_ABS, then the first line of the
583 * else section would cause a core dump, because its a reference
584 * beyond the end of the section array.
585 * This behaviour is exhibited by this code:
586 * GLOBAL crash_nasm
587 * crash_nasm equ 0
588 * To avoid such a crash, such requests are silently discarded.
589 * This may not be the best solution.
591 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
592 bsym = raa_write(bsym, segment, nglobs);
593 } else if (sym->section != SHN_ABS) {
595 * This is a global symbol; so we must add it to the rbtree
596 * of global symbols in its section.
598 * In addition, we check the special text for symbol
599 * type and size information.
601 sects[sym->section-1]->gsyms =
602 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
604 if (special) {
605 int n = strcspn(special, " \t");
607 if (!nasm_strnicmp(special, "function", n))
608 sym->type |= STT_FUNC;
609 else if (!nasm_strnicmp(special, "data", n) ||
610 !nasm_strnicmp(special, "object", n))
611 sym->type |= STT_OBJECT;
612 else if (!nasm_strnicmp(special, "notype", n))
613 sym->type |= STT_NOTYPE;
614 else
615 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
616 n, special);
617 special += n;
619 while (nasm_isspace(*special))
620 ++special;
621 if (*special) {
622 n = strcspn(special, " \t");
623 if (!nasm_strnicmp(special, "default", n))
624 sym->other = STV_DEFAULT;
625 else if (!nasm_strnicmp(special, "internal", n))
626 sym->other = STV_INTERNAL;
627 else if (!nasm_strnicmp(special, "hidden", n))
628 sym->other = STV_HIDDEN;
629 else if (!nasm_strnicmp(special, "protected", n))
630 sym->other = STV_PROTECTED;
631 else
632 n = 0;
633 special += n;
636 if (*special) {
637 struct tokenval tokval;
638 expr *e;
639 int fwd = 0;
640 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
642 while (special[n] && nasm_isspace(special[n]))
643 n++;
645 * We have a size expression; attempt to
646 * evaluate it.
648 stdscan_reset();
649 stdscan_bufptr = special + n;
650 tokval.t_type = TOKEN_INVALID;
651 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, nasm_error,
652 NULL);
653 if (fwd) {
654 sym->nextfwd = fwds;
655 fwds = sym;
656 sym->name = nasm_strdup(name);
657 } else if (e) {
658 if (!is_simple(e))
659 nasm_error(ERR_NONFATAL, "cannot use relocatable"
660 " expression as symbol size");
661 else
662 sym->size = reloc_value(e);
664 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
666 special_used = true;
669 * If TLS segment, mark symbol accordingly.
671 if (sects[sym->section - 1]->flags & SHF_TLS) {
672 sym->type &= 0xf0;
673 sym->type |= STT_TLS;
676 sym->globnum = nglobs;
677 nglobs++;
678 } else
679 nlocals++;
681 if (special && !special_used)
682 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
685 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
687 struct Reloc *r;
689 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
690 sect->tail = &r->next;
691 r->next = NULL;
693 r->address = sect->len;
694 if (segment == NO_SEG)
695 r->symbol = 0;
696 else {
697 int i;
698 r->symbol = 0;
699 for (i = 0; i < nsects; i++)
700 if (segment == sects[i]->index)
701 r->symbol = i + 2;
702 if (!r->symbol)
703 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
705 r->type = type;
707 sect->nrelocs++;
711 * This routine deals with ..got and ..sym relocations: the more
712 * complicated kinds. In shared-library writing, some relocations
713 * with respect to global symbols must refer to the precise symbol
714 * rather than referring to an offset from the base of the section
715 * _containing_ the symbol. Such relocations call to this routine,
716 * which searches the symbol list for the symbol in question.
718 * R_386_GOT32 references require the _exact_ symbol address to be
719 * used; R_386_32 references can be at an offset from the symbol.
720 * The boolean argument `exact' tells us this.
722 * Return value is the adjusted value of `addr', having become an
723 * offset from the symbol rather than the section. Should always be
724 * zero when returning from an exact call.
726 * Limitation: if you define two symbols at the same place,
727 * confusion will occur.
729 * Inefficiency: we search, currently, using a linked list which
730 * isn't even necessarily sorted.
732 static int32_t elf_add_gsym_reloc(struct Section *sect,
733 int32_t segment, uint32_t offset,
734 int type, bool exact)
736 struct Reloc *r;
737 struct Section *s;
738 struct Symbol *sym;
739 struct rbtree *srb;
740 int i;
743 * First look up the segment/offset pair and find a global
744 * symbol corresponding to it. If it's not one of our segments,
745 * then it must be an external symbol, in which case we're fine
746 * doing a normal elf_add_reloc after first sanity-checking
747 * that the offset from the symbol is zero.
749 s = NULL;
750 for (i = 0; i < nsects; i++)
751 if (segment == sects[i]->index) {
752 s = sects[i];
753 break;
755 if (!s) {
756 if (exact && offset != 0)
757 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
758 " for this reference");
759 else
760 elf_add_reloc(sect, segment, type);
761 return offset;
764 srb = rb_search(s->gsyms, offset);
765 if (!srb || (exact && srb->key != offset)) {
766 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
767 " for this reference");
768 return 0;
770 sym = container_of(srb, struct Symbol, symv);
772 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
773 sect->tail = &r->next;
774 r->next = NULL;
776 r->address = sect->len;
777 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
778 r->type = type;
780 sect->nrelocs++;
782 return offset - sym->symv.key;
785 static void elf_out(int32_t segto, const void *data,
786 enum out_type type, uint64_t size,
787 int32_t segment, int32_t wrt)
789 struct Section *s;
790 int32_t addr;
791 uint8_t mydata[4], *p;
792 int i;
793 static struct symlininfo sinfo;
796 * handle absolute-assembly (structure definitions)
798 if (segto == NO_SEG) {
799 if (type != OUT_RESERVE)
800 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
801 " space");
802 return;
805 s = NULL;
806 for (i = 0; i < nsects; i++)
807 if (segto == sects[i]->index) {
808 s = sects[i];
809 break;
811 if (!s) {
812 int tempint; /* ignored */
813 if (segto != elf_section_names(".text", 2, &tempint))
814 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
815 else {
816 s = sects[nsects - 1];
817 i = nsects - 1;
821 /* again some stabs debugging stuff */
822 if (of_elf32.current_dfmt) {
823 sinfo.offset = s->len;
824 sinfo.section = i;
825 sinfo.name = s->name;
826 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
828 /* end of debugging stuff */
830 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
831 nasm_error(ERR_WARNING, "attempt to initialize memory in"
832 " BSS section `%s': ignored", s->name);
833 s->len += realsize(type, size);
834 return;
837 if (type == OUT_RESERVE) {
838 if (s->type == SHT_PROGBITS) {
839 nasm_error(ERR_WARNING, "uninitialized space declared in"
840 " non-BSS section `%s': zeroing", s->name);
841 elf_sect_write(s, NULL, size);
842 } else
843 s->len += size;
844 } else if (type == OUT_RAWDATA) {
845 if (segment != NO_SEG)
846 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
847 elf_sect_write(s, data, size);
848 } else if (type == OUT_ADDRESS) {
849 bool gnu16 = false;
850 addr = *(int64_t *)data;
851 if (segment != NO_SEG) {
852 if (segment % 2) {
853 nasm_error(ERR_NONFATAL, "ELF format does not support"
854 " segment base references");
855 } else {
856 if (wrt == NO_SEG) {
857 if (size == 2) {
858 gnu16 = true;
859 elf_add_reloc(s, segment, R_386_16);
860 } else {
861 elf_add_reloc(s, segment, R_386_32);
863 } else if (wrt == elf_gotpc_sect + 1) {
865 * The user will supply GOT relative to $$. ELF
866 * will let us have GOT relative to $. So we
867 * need to fix up the data item by $-$$.
869 addr += s->len;
870 elf_add_reloc(s, segment, R_386_GOTPC);
871 } else if (wrt == elf_gotoff_sect + 1) {
872 elf_add_reloc(s, segment, R_386_GOTOFF);
873 } else if (wrt == elf_tlsie_sect + 1) {
874 addr = elf_add_gsym_reloc(s, segment, addr,
875 R_386_TLS_IE, true);
876 } else if (wrt == elf_got_sect + 1) {
877 addr = elf_add_gsym_reloc(s, segment, addr,
878 R_386_GOT32, true);
879 } else if (wrt == elf_sym_sect + 1) {
880 if (size == 2) {
881 gnu16 = true;
882 addr = elf_add_gsym_reloc(s, segment, addr,
883 R_386_16, false);
884 } else {
885 addr = elf_add_gsym_reloc(s, segment, addr,
886 R_386_32, false);
888 } else if (wrt == elf_plt_sect + 1) {
889 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
890 "relative PLT references");
891 } else {
892 nasm_error(ERR_NONFATAL, "ELF format does not support this"
893 " use of WRT");
894 wrt = NO_SEG; /* we can at least _try_ to continue */
898 p = mydata;
899 if (gnu16) {
900 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
901 "16-bit relocations in ELF is a GNU extension");
902 WRITESHORT(p, addr);
903 } else {
904 if (size != 4 && segment != NO_SEG) {
905 nasm_error(ERR_NONFATAL,
906 "Unsupported non-32-bit ELF relocation");
908 WRITELONG(p, addr);
910 elf_sect_write(s, mydata, size);
911 } else if (type == OUT_REL2ADR) {
912 if (segment == segto)
913 nasm_error(ERR_PANIC, "intra-segment OUT_REL2ADR");
914 if (segment != NO_SEG && segment % 2) {
915 nasm_error(ERR_NONFATAL, "ELF format does not support"
916 " segment base references");
917 } else {
918 if (wrt == NO_SEG) {
919 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
920 "16-bit relocations in ELF is a GNU extension");
921 elf_add_reloc(s, segment, R_386_PC16);
922 } else {
923 nasm_error(ERR_NONFATAL,
924 "Unsupported non-32-bit ELF relocation");
927 p = mydata;
928 WRITESHORT(p, *(int64_t *)data - size);
929 elf_sect_write(s, mydata, 2L);
930 } else if (type == OUT_REL4ADR) {
931 if (segment == segto)
932 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
933 if (segment != NO_SEG && segment % 2) {
934 nasm_error(ERR_NONFATAL, "ELF format does not support"
935 " segment base references");
936 } else {
937 if (wrt == NO_SEG) {
938 elf_add_reloc(s, segment, R_386_PC32);
939 } else if (wrt == elf_plt_sect + 1) {
940 elf_add_reloc(s, segment, R_386_PLT32);
941 } else if (wrt == elf_gotpc_sect + 1 ||
942 wrt == elf_gotoff_sect + 1 ||
943 wrt == elf_got_sect + 1) {
944 nasm_error(ERR_NONFATAL, "ELF format cannot produce PC-"
945 "relative GOT references");
946 } else {
947 nasm_error(ERR_NONFATAL, "ELF format does not support this"
948 " use of WRT");
949 wrt = NO_SEG; /* we can at least _try_ to continue */
952 p = mydata;
953 WRITELONG(p, *(int64_t *)data - size);
954 elf_sect_write(s, mydata, 4L);
958 static void elf_write(void)
960 int align;
961 char *p;
962 int i;
964 struct SAA *symtab;
965 int32_t symtablen, symtablocal;
968 * Work out how many sections we will have. We have SHN_UNDEF,
969 * then the flexible user sections, then the fixed sections
970 * `.shstrtab', `.symtab' and `.strtab', then optionally
971 * relocation sections for the user sections.
973 nsections = sec_numspecial + 1;
974 if (of_elf32.current_dfmt == &df_stabs)
975 nsections += 3;
976 else if (of_elf32.current_dfmt == &df_dwarf)
977 nsections += 10;
979 add_sectname("", ".shstrtab");
980 add_sectname("", ".symtab");
981 add_sectname("", ".strtab");
982 for (i = 0; i < nsects; i++) {
983 nsections++; /* for the section itself */
984 if (sects[i]->head) {
985 nsections++; /* for its relocations */
986 add_sectname(".rel", sects[i]->name);
990 if (of_elf32.current_dfmt == &df_stabs) {
991 /* in case the debug information is wanted, just add these three sections... */
992 add_sectname("", ".stab");
993 add_sectname("", ".stabstr");
994 add_sectname(".rel", ".stab");
995 } else if (of_elf32.current_dfmt == &df_dwarf) {
996 /* the dwarf debug standard specifies the following ten sections,
997 not all of which are currently implemented,
998 although all of them are defined. */
999 add_sectname("", ".debug_aranges");
1000 add_sectname(".rela", ".debug_aranges");
1001 add_sectname("", ".debug_pubnames");
1002 add_sectname("", ".debug_info");
1003 add_sectname(".rela", ".debug_info");
1004 add_sectname("", ".debug_abbrev");
1005 add_sectname("", ".debug_line");
1006 add_sectname(".rela", ".debug_line");
1007 add_sectname("", ".debug_frame");
1008 add_sectname("", ".debug_loc");
1012 * Output the ELF header.
1014 fwrite("\177ELF\1\1\1", 7, 1, ofile);
1015 fputc(elf_osabi, ofile);
1016 fputc(elf_abiver, ofile);
1017 fwritezero(7, ofile);
1018 fwriteint16_t(1, ofile); /* ET_REL relocatable file */
1019 fwriteint16_t(3, ofile); /* EM_386 processor ID */
1020 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
1021 fwriteint32_t(0L, ofile); /* no entry point */
1022 fwriteint32_t(0L, ofile); /* no program header table */
1023 fwriteint32_t(0x40L, ofile); /* section headers straight after
1024 * ELF header plus alignment */
1025 fwriteint32_t(0L, ofile); /* 386 defines no special flags */
1026 fwriteint16_t(0x34, ofile); /* size of ELF header */
1027 fwriteint16_t(0, ofile); /* no program header table, again */
1028 fwriteint16_t(0, ofile); /* still no program header table */
1029 fwriteint16_t(0x28, ofile); /* size of section header */
1030 fwriteint16_t(nsections, ofile); /* number of sections */
1031 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
1032 * section header table */
1033 fwriteint32_t(0L, ofile); /* align to 0x40 bytes */
1034 fwriteint32_t(0L, ofile);
1035 fwriteint32_t(0L, ofile);
1038 * Build the symbol table and relocation tables.
1040 symtab = elf_build_symtab(&symtablen, &symtablocal);
1041 for (i = 0; i < nsects; i++)
1042 if (sects[i]->head)
1043 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1044 sects[i]->head);
1047 * Now output the section header table.
1050 elf_foffs = 0x40 + 0x28 * nsections;
1051 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1052 elf_foffs += align;
1053 elf_nsect = 0;
1054 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1056 /* SHN_UNDEF */
1057 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1058 p = shstrtab + 1;
1060 /* The normal sections */
1061 for (i = 0; i < nsects; i++) {
1062 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1063 (sects[i]->type == SHT_PROGBITS ?
1064 sects[i]->data : NULL), true,
1065 sects[i]->len, 0, 0, sects[i]->align, 0);
1066 p += strlen(p) + 1;
1069 /* .shstrtab */
1070 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1071 shstrtablen, 0, 0, 1, 0);
1072 p += strlen(p) + 1;
1074 /* .symtab */
1075 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1076 symtablen, sec_strtab, symtablocal, 4, 16);
1077 p += strlen(p) + 1;
1079 /* .strtab */
1080 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1081 strslen, 0, 0, 1, 0);
1082 p += strlen(p) + 1;
1084 /* The relocation sections */
1085 for (i = 0; i < nsects; i++)
1086 if (sects[i]->head) {
1087 elf_section_header(p - shstrtab, SHT_REL, 0, sects[i]->rel, true,
1088 sects[i]->rellen, sec_symtab, i + 1, 4, 8);
1089 p += strlen(p) + 1;
1093 if (of_elf32.current_dfmt == &df_stabs) {
1094 /* for debugging information, create the last three sections
1095 which are the .stab , .stabstr and .rel.stab sections respectively */
1097 /* this function call creates the stab sections in memory */
1098 stabs32_generate();
1100 if (stabbuf && stabstrbuf && stabrelbuf) {
1101 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1102 stablen, sec_stabstr, 0, 4, 12);
1103 p += strlen(p) + 1;
1105 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1106 stabstrlen, 0, 0, 4, 0);
1107 p += strlen(p) + 1;
1109 /* link -> symtable info -> section to refer to */
1110 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1111 stabrellen, sec_symtab, sec_stab, 4, 8);
1112 p += strlen(p) + 1;
1114 } else if (of_elf32.current_dfmt == &df_dwarf) {
1115 /* for dwarf debugging information, create the ten dwarf sections */
1117 /* this function call creates the dwarf sections in memory */
1118 if (dwarf_fsect)
1119 dwarf32_generate();
1121 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1122 arangeslen, 0, 0, 1, 0);
1123 p += strlen(p) + 1;
1125 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1126 arangesrellen, sec_symtab, sec_debug_aranges,
1127 1, 12);
1128 p += strlen(p) + 1;
1130 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf,
1131 false, pubnameslen, 0, 0, 1, 0);
1132 p += strlen(p) + 1;
1134 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1135 infolen, 0, 0, 1, 0);
1136 p += strlen(p) + 1;
1138 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1139 inforellen, sec_symtab, sec_debug_info, 1, 12);
1140 p += strlen(p) + 1;
1142 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1143 abbrevlen, 0, 0, 1, 0);
1144 p += strlen(p) + 1;
1146 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1147 linelen, 0, 0, 1, 0);
1148 p += strlen(p) + 1;
1150 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1151 linerellen, sec_symtab, sec_debug_line, 1, 12);
1152 p += strlen(p) + 1;
1154 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1155 framelen, 0, 0, 8, 0);
1156 p += strlen(p) + 1;
1158 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1159 loclen, 0, 0, 1, 0);
1160 p += strlen(p) + 1;
1162 fwritezero(align, ofile);
1165 * Now output the sections.
1167 elf_write_sections();
1169 nasm_free(elf_sects);
1170 saa_free(symtab);
1173 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1175 struct SAA *s = saa_init(1L);
1176 struct Symbol *sym;
1177 uint8_t entry[16], *p;
1178 int i;
1180 *len = *local = 0;
1183 * First, an all-zeros entry, required by the ELF spec.
1185 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1186 *len += 16;
1187 (*local)++;
1190 * Next, an entry for the file name.
1192 p = entry;
1193 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1194 WRITELONG(p, 0); /* no value */
1195 WRITELONG(p, 0); /* no size either */
1196 WRITESHORT(p, STT_FILE); /* type FILE */
1197 WRITESHORT(p, SHN_ABS);
1198 saa_wbytes(s, entry, 16L);
1199 *len += 16;
1200 (*local)++;
1203 * Now some standard symbols defining the segments, for relocation
1204 * purposes.
1206 for (i = 1; i <= nsects; i++) {
1207 p = entry;
1208 WRITELONG(p, 0); /* no symbol name */
1209 WRITELONG(p, 0); /* offset zero */
1210 WRITELONG(p, 0); /* size zero */
1211 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1212 WRITESHORT(p, i); /* section id */
1213 saa_wbytes(s, entry, 16L);
1214 *len += 16;
1215 (*local)++;
1219 * Now the other local symbols.
1221 saa_rewind(syms);
1222 while ((sym = saa_rstruct(syms))) {
1223 if (sym->type & SYM_GLOBAL)
1224 continue;
1225 p = entry;
1226 WRITELONG(p, sym->strpos);
1227 WRITELONG(p, sym->symv.key);
1228 WRITELONG(p, sym->size);
1229 WRITECHAR(p, sym->type); /* type and binding */
1230 WRITECHAR(p, sym->other); /* visibility */
1231 WRITESHORT(p, sym->section);
1232 saa_wbytes(s, entry, 16L);
1233 *len += 16;
1234 (*local)++;
1237 * dwarf needs symbols for debug sections
1238 * which are relocation targets.
1240 //*** fix for 32 bit
1241 if (of_elf32.current_dfmt == &df_dwarf) {
1242 dwarf_infosym = *local;
1243 p = entry;
1244 WRITELONG(p, 0); /* no symbol name */
1245 WRITELONG(p, (uint32_t) 0); /* offset zero */
1246 WRITELONG(p, (uint32_t) 0); /* size zero */
1247 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1248 WRITESHORT(p, sec_debug_info); /* section id */
1249 saa_wbytes(s, entry, 16L);
1250 *len += 16;
1251 (*local)++;
1252 dwarf_abbrevsym = *local;
1253 p = entry;
1254 WRITELONG(p, 0); /* no symbol name */
1255 WRITELONG(p, (uint32_t) 0); /* offset zero */
1256 WRITELONG(p, (uint32_t) 0); /* size zero */
1257 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1258 WRITESHORT(p, sec_debug_abbrev); /* section id */
1259 saa_wbytes(s, entry, 16L);
1260 *len += 16;
1261 (*local)++;
1262 dwarf_linesym = *local;
1263 p = entry;
1264 WRITELONG(p, 0); /* no symbol name */
1265 WRITELONG(p, (uint32_t) 0); /* offset zero */
1266 WRITELONG(p, (uint32_t) 0); /* size zero */
1267 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1268 WRITESHORT(p, sec_debug_line); /* section id */
1269 saa_wbytes(s, entry, 16L);
1270 *len += 16;
1271 (*local)++;
1275 * Now the global symbols.
1277 saa_rewind(syms);
1278 while ((sym = saa_rstruct(syms))) {
1279 if (!(sym->type & SYM_GLOBAL))
1280 continue;
1281 p = entry;
1282 WRITELONG(p, sym->strpos);
1283 WRITELONG(p, sym->symv.key);
1284 WRITELONG(p, sym->size);
1285 WRITECHAR(p, sym->type); /* type and binding */
1286 WRITECHAR(p, sym->other); /* visibility */
1287 WRITESHORT(p, sym->section);
1288 saa_wbytes(s, entry, 16L);
1289 *len += 16;
1292 return s;
1295 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1297 struct SAA *s;
1298 uint8_t *p, entry[8];
1299 int32_t global_offset;
1301 if (!r)
1302 return NULL;
1304 s = saa_init(1L);
1305 *len = 0;
1308 * How to onvert from a global placeholder to a real symbol index;
1309 * the +2 refers to the two special entries, the null entry and
1310 * the filename entry.
1312 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1314 while (r) {
1315 int32_t sym = r->symbol;
1318 * Create a real symbol index; the +2 refers to the two special
1319 * entries, the null entry and the filename entry.
1321 if (sym >= GLOBAL_TEMP_BASE)
1322 sym += global_offset;
1324 p = entry;
1325 WRITELONG(p, r->address);
1326 WRITELONG(p, (sym << 8) + r->type);
1327 saa_wbytes(s, entry, 8L);
1328 *len += 8;
1330 r = r->next;
1333 return s;
1336 static void elf_section_header(int name, int type, int flags,
1337 void *data, bool is_saa, int32_t datalen,
1338 int link, int info, int align, int eltsize)
1340 elf_sects[elf_nsect].data = data;
1341 elf_sects[elf_nsect].len = datalen;
1342 elf_sects[elf_nsect].is_saa = is_saa;
1343 elf_nsect++;
1345 fwriteint32_t((int32_t)name, ofile);
1346 fwriteint32_t((int32_t)type, ofile);
1347 fwriteint32_t((int32_t)flags, ofile);
1348 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
1349 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
1350 fwriteint32_t(datalen, ofile);
1351 if (data)
1352 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1353 fwriteint32_t((int32_t)link, ofile);
1354 fwriteint32_t((int32_t)info, ofile);
1355 fwriteint32_t((int32_t)align, ofile);
1356 fwriteint32_t((int32_t)eltsize, ofile);
1359 static void elf_write_sections(void)
1361 int i;
1362 for (i = 0; i < elf_nsect; i++)
1363 if (elf_sects[i].data) {
1364 int32_t len = elf_sects[i].len;
1365 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1366 int32_t align = reallen - len;
1367 if (elf_sects[i].is_saa)
1368 saa_fpwrite(elf_sects[i].data, ofile);
1369 else
1370 fwrite(elf_sects[i].data, len, 1, ofile);
1371 fwritezero(align, ofile);
1375 static void elf_sect_write(struct Section *sect,
1376 const uint8_t *data, uint32_t len)
1378 saa_wbytes(sect->data, data, len);
1379 sect->len += len;
1382 static int32_t elf_segbase(int32_t segment)
1384 return segment;
1387 static int elf_directive(enum directives directive, char *value, int pass)
1389 bool err;
1390 int64_t n;
1391 char *p;
1393 switch (directive) {
1394 case D_OSABI:
1395 if (pass == 2)
1396 return 1; /* ignore in pass 2 */
1398 n = readnum(value, &err);
1399 if (err) {
1400 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1401 return 1;
1403 if (n < 0 || n > 255) {
1404 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1405 return 1;
1407 elf_osabi = n;
1408 elf_abiver = 0;
1410 if ((p = strchr(value,',')) == NULL)
1411 return 1;
1413 n = readnum(p+1, &err);
1414 if (err || n < 0 || n > 255) {
1415 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1416 return 1;
1419 elf_abiver = n;
1420 return 1;
1422 default:
1423 return 0;
1427 static void elf_filename(char *inname, char *outname)
1429 strcpy(elf_module, inname);
1430 standard_extension(inname, outname, ".o");
1433 extern macros_t elf_stdmac[];
1435 static int elf_set_info(enum geninfo type, char **val)
1437 (void)type;
1438 (void)val;
1439 return 0;
1441 static struct dfmt df_dwarf = {
1442 "ELF32 (i386) dwarf debug format for Linux/Unix",
1443 "dwarf",
1444 dwarf32_init,
1445 dwarf32_linenum,
1446 debug32_deflabel,
1447 debug32_directive,
1448 debug32_typevalue,
1449 dwarf32_output,
1450 dwarf32_cleanup
1452 static struct dfmt df_stabs = {
1453 "ELF32 (i386) stabs debug format for Linux/Unix",
1454 "stabs",
1455 null_debug_init,
1456 stabs32_linenum,
1457 debug32_deflabel,
1458 debug32_directive,
1459 debug32_typevalue,
1460 stabs32_output,
1461 stabs32_cleanup
1464 struct dfmt *elf32_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1466 struct ofmt of_elf32 = {
1467 "ELF32 (i386) object files (e.g. Linux)",
1468 "elf32",
1470 elf32_debugs_arr,
1471 &df_stabs,
1472 elf_stdmac,
1473 elf_init,
1474 elf_set_info,
1475 elf_out,
1476 elf_deflabel,
1477 elf_section_names,
1478 elf_segbase,
1479 elf_directive,
1480 elf_filename,
1481 elf_cleanup
1484 struct ofmt of_elf = {
1485 "ELF (short name for ELF32) ",
1486 "elf",
1488 elf32_debugs_arr,
1489 &df_stabs,
1490 elf_stdmac,
1491 elf_init_hack,
1492 elf_set_info,
1493 elf_out,
1494 elf_deflabel,
1495 elf_section_names,
1496 elf_segbase,
1497 elf_directive,
1498 elf_filename,
1499 elf_cleanup
1501 /* again, the stabs debugging stuff (code) */
1503 static void stabs32_linenum(const char *filename, int32_t linenumber,
1504 int32_t segto)
1506 (void)segto;
1508 if (!stabs_filename) {
1509 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1510 strcpy(stabs_filename, filename);
1511 } else {
1512 if (strcmp(stabs_filename, filename)) {
1513 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1514 in fact, this leak comes in quite handy to maintain a list of files
1515 encountered so far in the symbol lines... */
1517 /* why not nasm_free(stabs_filename); we're done with the old one */
1519 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1520 strcpy(stabs_filename, filename);
1523 debug_immcall = 1;
1524 currentline = linenumber;
1527 static void debug32_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1528 char *special)
1530 (void)name;
1531 (void)segment;
1532 (void)offset;
1533 (void)is_global;
1534 (void)special;
1537 static void debug32_directive(const char *directive, const char *params)
1539 (void)directive;
1540 (void)params;
1543 static void debug32_typevalue(int32_t type)
1545 int32_t stype, ssize;
1546 switch (TYM_TYPE(type)) {
1547 case TY_LABEL:
1548 ssize = 0;
1549 stype = STT_NOTYPE;
1550 break;
1551 case TY_BYTE:
1552 ssize = 1;
1553 stype = STT_OBJECT;
1554 break;
1555 case TY_WORD:
1556 ssize = 2;
1557 stype = STT_OBJECT;
1558 break;
1559 case TY_DWORD:
1560 ssize = 4;
1561 stype = STT_OBJECT;
1562 break;
1563 case TY_FLOAT:
1564 ssize = 4;
1565 stype = STT_OBJECT;
1566 break;
1567 case TY_QWORD:
1568 ssize = 8;
1569 stype = STT_OBJECT;
1570 break;
1571 case TY_TBYTE:
1572 ssize = 10;
1573 stype = STT_OBJECT;
1574 break;
1575 case TY_OWORD:
1576 ssize = 16;
1577 stype = STT_OBJECT;
1578 break;
1579 case TY_YWORD:
1580 ssize = 32;
1581 stype = STT_OBJECT;
1582 break;
1583 case TY_COMMON:
1584 ssize = 0;
1585 stype = STT_COMMON;
1586 break;
1587 case TY_SEG:
1588 ssize = 0;
1589 stype = STT_SECTION;
1590 break;
1591 case TY_EXTERN:
1592 ssize = 0;
1593 stype = STT_NOTYPE;
1594 break;
1595 case TY_EQU:
1596 ssize = 0;
1597 stype = STT_NOTYPE;
1598 break;
1599 default:
1600 ssize = 0;
1601 stype = STT_NOTYPE;
1602 break;
1604 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1605 lastsym->size = ssize;
1606 lastsym->type = stype;
1610 static void stabs32_output(int type, void *param)
1612 struct symlininfo *s;
1613 struct linelist *el;
1614 if (type == TY_STABSSYMLIN) {
1615 if (debug_immcall) {
1616 s = (struct symlininfo *)param;
1617 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1618 return; /* we are only interested in the text stuff */
1619 numlinestabs++;
1620 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1621 el->info.offset = s->offset;
1622 el->info.section = s->section;
1623 el->info.name = s->name;
1624 el->line = currentline;
1625 el->filename = stabs_filename;
1626 el->next = 0;
1627 if (stabslines) {
1628 stabslines->last->next = el;
1629 stabslines->last = el;
1630 } else {
1631 stabslines = el;
1632 stabslines->last = el;
1636 debug_immcall = 0;
1639 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1640 do {\
1641 WRITELONG(p,n_strx); \
1642 WRITECHAR(p,n_type); \
1643 WRITECHAR(p,n_other); \
1644 WRITESHORT(p,n_desc); \
1645 WRITELONG(p,n_value); \
1646 } while (0)
1648 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1650 static void stabs32_generate(void)
1652 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1653 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1654 char **allfiles;
1655 int *fileidx;
1657 struct linelist *ptr;
1659 ptr = stabslines;
1661 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1662 for (i = 0; i < numlinestabs; i++)
1663 allfiles[i] = 0;
1664 numfiles = 0;
1665 while (ptr) {
1666 if (numfiles == 0) {
1667 allfiles[0] = ptr->filename;
1668 numfiles++;
1669 } else {
1670 for (i = 0; i < numfiles; i++) {
1671 if (!strcmp(allfiles[i], ptr->filename))
1672 break;
1674 if (i >= numfiles) {
1675 allfiles[i] = ptr->filename;
1676 numfiles++;
1679 ptr = ptr->next;
1681 strsize = 1;
1682 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1683 for (i = 0; i < numfiles; i++) {
1684 fileidx[i] = strsize;
1685 strsize += strlen(allfiles[i]) + 1;
1687 mainfileindex = 0;
1688 for (i = 0; i < numfiles; i++) {
1689 if (!strcmp(allfiles[i], elf_module)) {
1690 mainfileindex = i;
1691 break;
1695 /* worst case size of the stab buffer would be:
1696 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1698 sbuf =
1699 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1700 sizeof(struct stabentry));
1702 ssbuf = (uint8_t *)nasm_malloc(strsize);
1704 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1705 rptr = rbuf;
1707 for (i = 0; i < numfiles; i++) {
1708 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1710 ssbuf[0] = 0;
1712 stabstrlen = strsize; /* set global variable for length of stab strings */
1714 sptr = sbuf;
1715 ptr = stabslines;
1716 numstabs = 0;
1718 if (ptr) {
1719 /* this is the first stab, its strx points to the filename of the
1720 the source-file, the n_desc field should be set to the number
1721 of remaining stabs
1723 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1725 /* this is the stab for the main source file */
1726 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1728 /* relocation table entry */
1730 /* Since the symbol table has two entries before */
1731 /* the section symbols, the index in the info.section */
1732 /* member must be adjusted by adding 2 */
1734 WRITELONG(rptr, (sptr - sbuf) - 4);
1735 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1737 numstabs++;
1738 currfile = mainfileindex;
1741 while (ptr) {
1742 if (strcmp(allfiles[currfile], ptr->filename)) {
1743 /* oops file has changed... */
1744 for (i = 0; i < numfiles; i++)
1745 if (!strcmp(allfiles[i], ptr->filename))
1746 break;
1747 currfile = i;
1748 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1749 ptr->info.offset);
1750 numstabs++;
1752 /* relocation table entry */
1753 WRITELONG(rptr, (sptr - sbuf) - 4);
1754 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1757 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1758 numstabs++;
1760 /* relocation table entry */
1762 WRITELONG(rptr, (sptr - sbuf) - 4);
1763 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1765 ptr = ptr->next;
1769 ((struct stabentry *)sbuf)->n_desc = numstabs;
1771 nasm_free(allfiles);
1772 nasm_free(fileidx);
1774 stablen = (sptr - sbuf);
1775 stabrellen = (rptr - rbuf);
1776 stabrelbuf = rbuf;
1777 stabbuf = sbuf;
1778 stabstrbuf = ssbuf;
1781 static void stabs32_cleanup(void)
1783 struct linelist *ptr, *del;
1784 if (!stabslines)
1785 return;
1786 ptr = stabslines;
1787 while (ptr) {
1788 del = ptr;
1789 ptr = ptr->next;
1790 nasm_free(del);
1792 if (stabbuf)
1793 nasm_free(stabbuf);
1794 if (stabrelbuf)
1795 nasm_free(stabrelbuf);
1796 if (stabstrbuf)
1797 nasm_free(stabstrbuf);
1800 /* dwarf routines */
1802 static void dwarf32_init(void)
1804 ndebugs = 3; /* 3 debug symbols */
1807 static void dwarf32_linenum(const char *filename, int32_t linenumber,
1808 int32_t segto)
1810 (void)segto;
1811 dwarf32_findfile(filename);
1812 debug_immcall = 1;
1813 currentline = linenumber;
1816 /* called from elf_out with type == TY_DEBUGSYMLIN */
1817 static void dwarf32_output(int type, void *param)
1819 int ln, aa, inx, maxln, soc;
1820 struct symlininfo *s;
1821 struct SAA *plinep;
1823 (void)type;
1825 s = (struct symlininfo *)param;
1826 /* line number info is only gathered for executable sections */
1827 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1828 return;
1829 /* Check if section index has changed */
1830 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1832 dwarf32_findsect(s->section);
1834 /* do nothing unless line or file has changed */
1835 if (debug_immcall)
1837 ln = currentline - dwarf_csect->line;
1838 aa = s->offset - dwarf_csect->offset;
1839 inx = dwarf_clist->line;
1840 plinep = dwarf_csect->psaa;
1841 /* check for file change */
1842 if (!(inx == dwarf_csect->file))
1844 saa_write8(plinep,DW_LNS_set_file);
1845 saa_write8(plinep,inx);
1846 dwarf_csect->file = inx;
1848 /* check for line change */
1849 if (ln)
1851 /* test if in range of special op code */
1852 maxln = line_base + line_range;
1853 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1854 if (ln >= line_base && ln < maxln && soc < 256)
1856 saa_write8(plinep,soc);
1858 else
1860 if (ln)
1862 saa_write8(plinep,DW_LNS_advance_line);
1863 saa_wleb128s(plinep,ln);
1865 if (aa)
1867 saa_write8(plinep,DW_LNS_advance_pc);
1868 saa_wleb128u(plinep,aa);
1871 dwarf_csect->line = currentline;
1872 dwarf_csect->offset = s->offset;
1874 /* show change handled */
1875 debug_immcall = 0;
1880 static void dwarf32_generate(void)
1882 uint8_t *pbuf;
1883 int indx;
1884 struct linelist *ftentry;
1885 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1886 struct SAA *parangesrel, *plinesrel, *pinforel;
1887 struct sectlist *psect;
1888 size_t saalen, linepoff, totlen, highaddr;
1890 /* write epilogues for each line program range */
1891 /* and build aranges section */
1892 paranges = saa_init(1L);
1893 parangesrel = saa_init(1L);
1894 saa_write16(paranges,2); /* dwarf version */
1895 saa_write32(parangesrel, paranges->datalen+4);
1896 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
1897 saa_write32(parangesrel, 0);
1898 saa_write32(paranges,0); /* offset into info */
1899 saa_write8(paranges,4); /* pointer size */
1900 saa_write8(paranges,0); /* not segmented */
1901 saa_write32(paranges,0); /* padding */
1902 /* iterate though sectlist entries */
1903 psect = dwarf_fsect;
1904 totlen = 0;
1905 highaddr = 0;
1906 for (indx = 0; indx < dwarf_nsections; indx++)
1908 plinep = psect->psaa;
1909 /* Line Number Program Epilogue */
1910 saa_write8(plinep,2); /* std op 2 */
1911 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1912 saa_write8(plinep,DW_LNS_extended_op);
1913 saa_write8(plinep,1); /* operand length */
1914 saa_write8(plinep,DW_LNE_end_sequence);
1915 totlen += plinep->datalen;
1916 /* range table relocation entry */
1917 saa_write32(parangesrel, paranges->datalen + 4);
1918 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
1919 saa_write32(parangesrel, (uint32_t) 0);
1920 /* range table entry */
1921 saa_write32(paranges,0x0000); /* range start */
1922 saa_write32(paranges,sects[psect->section]->len); /* range length */
1923 highaddr += sects[psect->section]->len;
1924 /* done with this entry */
1925 psect = psect->next;
1927 saa_write32(paranges,0); /* null address */
1928 saa_write32(paranges,0); /* null length */
1929 saalen = paranges->datalen;
1930 arangeslen = saalen + 4;
1931 arangesbuf = pbuf = nasm_malloc(arangeslen);
1932 WRITELONG(pbuf,saalen); /* initial length */
1933 saa_rnbytes(paranges, pbuf, saalen);
1934 saa_free(paranges);
1936 /* build rela.aranges section */
1937 arangesrellen = saalen = parangesrel->datalen;
1938 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1939 saa_rnbytes(parangesrel, pbuf, saalen);
1940 saa_free(parangesrel);
1942 /* build pubnames section */
1943 ppubnames = saa_init(1L);
1944 saa_write16(ppubnames,3); /* dwarf version */
1945 saa_write32(ppubnames,0); /* offset into info */
1946 saa_write32(ppubnames,0); /* space used in info */
1947 saa_write32(ppubnames,0); /* end of list */
1948 saalen = ppubnames->datalen;
1949 pubnameslen = saalen + 4;
1950 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1951 WRITELONG(pbuf,saalen); /* initial length */
1952 saa_rnbytes(ppubnames, pbuf, saalen);
1953 saa_free(ppubnames);
1955 /* build info section */
1956 pinfo = saa_init(1L);
1957 pinforel = saa_init(1L);
1958 saa_write16(pinfo,2); /* dwarf version */
1959 saa_write32(pinforel, pinfo->datalen + 4);
1960 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
1961 saa_write32(pinforel, 0);
1962 saa_write32(pinfo,0); /* offset into abbrev */
1963 saa_write8(pinfo,4); /* pointer size */
1964 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1965 saa_write32(pinforel, pinfo->datalen + 4);
1966 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1967 saa_write32(pinforel, 0);
1968 saa_write32(pinfo,0); /* DW_AT_low_pc */
1969 saa_write32(pinforel, pinfo->datalen + 4);
1970 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1971 saa_write32(pinforel, 0);
1972 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
1973 saa_write32(pinforel, pinfo->datalen + 4);
1974 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
1975 saa_write32(pinforel, 0);
1976 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1977 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1978 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1979 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1980 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1981 saa_write32(pinforel, pinfo->datalen + 4);
1982 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1983 saa_write32(pinforel, 0);
1984 saa_write32(pinfo,0); /* DW_AT_low_pc */
1985 saa_write32(pinfo,0); /* DW_AT_frame_base */
1986 saa_write8(pinfo,0); /* end of entries */
1987 saalen = pinfo->datalen;
1988 infolen = saalen + 4;
1989 infobuf = pbuf = nasm_malloc(infolen);
1990 WRITELONG(pbuf,saalen); /* initial length */
1991 saa_rnbytes(pinfo, pbuf, saalen);
1992 saa_free(pinfo);
1994 /* build rela.info section */
1995 inforellen = saalen = pinforel->datalen;
1996 inforelbuf = pbuf = nasm_malloc(inforellen);
1997 saa_rnbytes(pinforel, pbuf, saalen);
1998 saa_free(pinforel);
2000 /* build abbrev section */
2001 pabbrev = saa_init(1L);
2002 saa_write8(pabbrev,1); /* entry number LEB128u */
2003 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2004 saa_write8(pabbrev,1); /* has children */
2005 /* the following attributes and forms are all LEB128u values */
2006 saa_write8(pabbrev,DW_AT_low_pc);
2007 saa_write8(pabbrev,DW_FORM_addr);
2008 saa_write8(pabbrev,DW_AT_high_pc);
2009 saa_write8(pabbrev,DW_FORM_addr);
2010 saa_write8(pabbrev,DW_AT_stmt_list);
2011 saa_write8(pabbrev,DW_FORM_data4);
2012 saa_write8(pabbrev,DW_AT_name);
2013 saa_write8(pabbrev,DW_FORM_string);
2014 saa_write8(pabbrev,DW_AT_producer);
2015 saa_write8(pabbrev,DW_FORM_string);
2016 saa_write8(pabbrev,DW_AT_language);
2017 saa_write8(pabbrev,DW_FORM_data2);
2018 saa_write16(pabbrev,0); /* end of entry */
2019 /* LEB128u usage same as above */
2020 saa_write8(pabbrev,2); /* entry number */
2021 saa_write8(pabbrev,DW_TAG_subprogram);
2022 saa_write8(pabbrev,0); /* no children */
2023 saa_write8(pabbrev,DW_AT_low_pc);
2024 saa_write8(pabbrev,DW_FORM_addr);
2025 saa_write8(pabbrev,DW_AT_frame_base);
2026 saa_write8(pabbrev,DW_FORM_data4);
2027 saa_write16(pabbrev,0); /* end of entry */
2028 abbrevlen = saalen = pabbrev->datalen;
2029 abbrevbuf = pbuf = nasm_malloc(saalen);
2030 saa_rnbytes(pabbrev, pbuf, saalen);
2031 saa_free(pabbrev);
2033 /* build line section */
2034 /* prolog */
2035 plines = saa_init(1L);
2036 saa_write8(plines,1); /* Minimum Instruction Length */
2037 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2038 saa_write8(plines,line_base); /* Line Base */
2039 saa_write8(plines,line_range); /* Line Range */
2040 saa_write8(plines,opcode_base); /* Opcode Base */
2041 /* standard opcode lengths (# of LEB128u operands) */
2042 saa_write8(plines,0); /* Std opcode 1 length */
2043 saa_write8(plines,1); /* Std opcode 2 length */
2044 saa_write8(plines,1); /* Std opcode 3 length */
2045 saa_write8(plines,1); /* Std opcode 4 length */
2046 saa_write8(plines,1); /* Std opcode 5 length */
2047 saa_write8(plines,0); /* Std opcode 6 length */
2048 saa_write8(plines,0); /* Std opcode 7 length */
2049 saa_write8(plines,0); /* Std opcode 8 length */
2050 saa_write8(plines,1); /* Std opcode 9 length */
2051 saa_write8(plines,0); /* Std opcode 10 length */
2052 saa_write8(plines,0); /* Std opcode 11 length */
2053 saa_write8(plines,1); /* Std opcode 12 length */
2054 /* Directory Table */
2055 saa_write8(plines,0); /* End of table */
2056 /* File Name Table */
2057 ftentry = dwarf_flist;
2058 for (indx = 0;indx<dwarf_numfiles;indx++)
2060 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2061 saa_write8(plines,0); /* directory LEB128u */
2062 saa_write8(plines,0); /* time LEB128u */
2063 saa_write8(plines,0); /* size LEB128u */
2064 ftentry = ftentry->next;
2066 saa_write8(plines,0); /* End of table */
2067 linepoff = plines->datalen;
2068 linelen = linepoff + totlen + 10;
2069 linebuf = pbuf = nasm_malloc(linelen);
2070 WRITELONG(pbuf,linelen-4); /* initial length */
2071 WRITESHORT(pbuf,3); /* dwarf version */
2072 WRITELONG(pbuf,linepoff); /* offset to line number program */
2073 /* write line header */
2074 saalen = linepoff;
2075 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2076 pbuf += linepoff;
2077 saa_free(plines);
2078 /* concatonate line program ranges */
2079 linepoff += 13;
2080 plinesrel = saa_init(1L);
2081 psect = dwarf_fsect;
2082 for (indx = 0; indx < dwarf_nsections; indx++)
2084 saa_write32(plinesrel, linepoff);
2085 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
2086 saa_write32(plinesrel, (uint32_t) 0);
2087 plinep = psect->psaa;
2088 saalen = plinep->datalen;
2089 saa_rnbytes(plinep, pbuf, saalen);
2090 pbuf += saalen;
2091 linepoff += saalen;
2092 saa_free(plinep);
2093 /* done with this entry */
2094 psect = psect->next;
2098 /* build rela.lines section */
2099 linerellen =saalen = plinesrel->datalen;
2100 linerelbuf = pbuf = nasm_malloc(linerellen);
2101 saa_rnbytes(plinesrel, pbuf, saalen);
2102 saa_free(plinesrel);
2104 /* build frame section */
2105 framelen = 4;
2106 framebuf = pbuf = nasm_malloc(framelen);
2107 WRITELONG(pbuf,framelen-4); /* initial length */
2109 /* build loc section */
2110 loclen = 16;
2111 locbuf = pbuf = nasm_malloc(loclen);
2112 WRITELONG(pbuf,0); /* null beginning offset */
2113 WRITELONG(pbuf,0); /* null ending offset */
2116 static void dwarf32_cleanup(void)
2118 if (arangesbuf)
2119 nasm_free(arangesbuf);
2120 if (arangesrelbuf)
2121 nasm_free(arangesrelbuf);
2122 if (pubnamesbuf)
2123 nasm_free(pubnamesbuf);
2124 if (infobuf)
2125 nasm_free(infobuf);
2126 if (inforelbuf)
2127 nasm_free(inforelbuf);
2128 if (abbrevbuf)
2129 nasm_free(abbrevbuf);
2130 if (linebuf)
2131 nasm_free(linebuf);
2132 if (linerelbuf)
2133 nasm_free(linerelbuf);
2134 if (framebuf)
2135 nasm_free(framebuf);
2136 if (locbuf)
2137 nasm_free(locbuf);
2139 static void dwarf32_findfile(const char * fname)
2141 int finx;
2142 struct linelist *match;
2144 /* return if fname is current file name */
2145 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2146 /* search for match */
2147 else
2149 match = 0;
2150 if (dwarf_flist)
2152 match = dwarf_flist;
2153 for (finx = 0; finx < dwarf_numfiles; finx++)
2155 if (!(strcmp(fname, match->filename)))
2157 dwarf_clist = match;
2158 return;
2162 /* add file name to end of list */
2163 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2164 dwarf_numfiles++;
2165 dwarf_clist->line = dwarf_numfiles;
2166 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2167 strcpy(dwarf_clist->filename,fname);
2168 dwarf_clist->next = 0;
2169 /* if first entry */
2170 if (!dwarf_flist)
2172 dwarf_flist = dwarf_elist = dwarf_clist;
2173 dwarf_clist->last = 0;
2175 /* chain to previous entry */
2176 else
2178 dwarf_elist->next = dwarf_clist;
2179 dwarf_elist = dwarf_clist;
2183 /* */
2184 static void dwarf32_findsect(const int index)
2186 int sinx;
2187 struct sectlist *match;
2188 struct SAA *plinep;
2189 /* return if index is current section index */
2190 if (dwarf_csect && (dwarf_csect->section == index))
2192 return;
2194 /* search for match */
2195 else
2197 match = 0;
2198 if (dwarf_fsect)
2200 match = dwarf_fsect;
2201 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2203 if ((match->section == index))
2205 dwarf_csect = match;
2206 return;
2208 match = match->next;
2211 /* add entry to end of list */
2212 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2213 dwarf_nsections++;
2214 dwarf_csect->psaa = plinep = saa_init(1L);
2215 dwarf_csect->line = 1;
2216 dwarf_csect->offset = 0;
2217 dwarf_csect->file = 1;
2218 dwarf_csect->section = index;
2219 dwarf_csect->next = 0;
2220 /* set relocatable address at start of line program */
2221 saa_write8(plinep,DW_LNS_extended_op);
2222 saa_write8(plinep,5); /* operand length */
2223 saa_write8(plinep,DW_LNE_set_address);
2224 saa_write32(plinep,0); /* Start Address */
2225 /* if first entry */
2226 if (!dwarf_fsect)
2228 dwarf_fsect = dwarf_esect = dwarf_csect;
2229 dwarf_csect->last = 0;
2231 /* chain to previous entry */
2232 else
2234 dwarf_esect->next = dwarf_csect;
2235 dwarf_esect = dwarf_csect;
2240 #endif /* OF_ELF */