Fix some format strings for nasm_error
[nasm.git] / output / outelf32.c
blob6902beaf58436749d8e0eff8cfc6929a395b4f10
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);
257 elf_gotoff_sect = seg_alloc();
258 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
259 elf_got_sect = seg_alloc();
260 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
261 elf_plt_sect = seg_alloc();
262 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
263 elf_sym_sect = seg_alloc();
264 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
265 elf_tlsie_sect = seg_alloc();
266 define_label("..tlsie", elf_tlsie_sect + 1, 0L, NULL, false, false);
268 def_seg = seg_alloc();
271 static void elf_init_hack(void)
273 of_elf32.current_dfmt = of_elf.current_dfmt; /* Sync debugging format */
274 elf_init();
277 static void elf_cleanup(int debuginfo)
279 struct Reloc *r;
280 int i;
282 (void)debuginfo;
284 elf_write();
285 for (i = 0; i < nsects; i++) {
286 if (sects[i]->type != SHT_NOBITS)
287 saa_free(sects[i]->data);
288 if (sects[i]->head)
289 saa_free(sects[i]->rel);
290 while (sects[i]->head) {
291 r = sects[i]->head;
292 sects[i]->head = sects[i]->head->next;
293 nasm_free(r);
296 nasm_free(sects);
297 saa_free(syms);
298 raa_free(bsym);
299 saa_free(strs);
300 if (of_elf32.current_dfmt) {
301 of_elf32.current_dfmt->cleanup();
305 static void add_sectname(char *firsthalf, char *secondhalf)
307 int len = strlen(firsthalf) + strlen(secondhalf);
308 while (shstrtablen + len + 1 > shstrtabsize)
309 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
310 strcpy(shstrtab + shstrtablen, firsthalf);
311 strcat(shstrtab + shstrtablen, secondhalf);
312 shstrtablen += len + 1;
315 static int elf_make_section(char *name, int type, int flags, int align)
317 struct Section *s;
319 s = nasm_malloc(sizeof(*s));
321 if (type != SHT_NOBITS)
322 s->data = saa_init(1L);
323 s->head = NULL;
324 s->tail = &s->head;
325 s->len = s->size = 0;
326 s->nrelocs = 0;
327 if (!strcmp(name, ".text"))
328 s->index = def_seg;
329 else
330 s->index = seg_alloc();
331 add_sectname("", name);
332 s->name = nasm_malloc(1 + strlen(name));
333 strcpy(s->name, name);
334 s->type = type;
335 s->flags = flags;
336 s->align = align;
337 s->gsyms = NULL;
339 if (nsects >= sectlen)
340 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
341 sects[nsects++] = s;
343 return nsects - 1;
347 static int32_t elf_section_names(char *name, int pass, int *bits)
349 char *p;
350 uint32_t flags, flags_and, flags_or;
351 uint32_t align;
352 int type, i;
355 * Default is 32 bits.
357 if (!name) {
358 *bits = 32;
359 return def_seg;
362 p = name;
363 while (*p && !nasm_isspace(*p))
364 p++;
365 if (*p)
366 *p++ = '\0';
367 flags_and = flags_or = type = align = 0;
369 while (*p && nasm_isspace(*p))
370 p++;
371 while (*p) {
372 char *q = p;
373 while (*p && !nasm_isspace(*p))
374 p++;
375 if (*p)
376 *p++ = '\0';
377 while (*p && nasm_isspace(*p))
378 p++;
380 if (!nasm_strnicmp(q, "align=", 6)) {
381 align = atoi(q + 6);
382 if (align == 0)
383 align = 1;
384 if ((align - 1) & align) { /* means it's not a power of two */
385 nasm_error(ERR_NONFATAL, "section alignment %d is not"
386 " a power of two", align);
387 align = 1;
389 } else if (!nasm_stricmp(q, "alloc")) {
390 flags_and |= SHF_ALLOC;
391 flags_or |= SHF_ALLOC;
392 } else if (!nasm_stricmp(q, "noalloc")) {
393 flags_and |= SHF_ALLOC;
394 flags_or &= ~SHF_ALLOC;
395 } else if (!nasm_stricmp(q, "exec")) {
396 flags_and |= SHF_EXECINSTR;
397 flags_or |= SHF_EXECINSTR;
398 } else if (!nasm_stricmp(q, "noexec")) {
399 flags_and |= SHF_EXECINSTR;
400 flags_or &= ~SHF_EXECINSTR;
401 } else if (!nasm_stricmp(q, "write")) {
402 flags_and |= SHF_WRITE;
403 flags_or |= SHF_WRITE;
404 } else if (!nasm_stricmp(q, "tls")) {
405 flags_and |= SHF_TLS;
406 flags_or |= SHF_TLS;
407 } else if (!nasm_stricmp(q, "nowrite")) {
408 flags_and |= SHF_WRITE;
409 flags_or &= ~SHF_WRITE;
410 } else if (!nasm_stricmp(q, "progbits")) {
411 type = SHT_PROGBITS;
412 } else if (!nasm_stricmp(q, "nobits")) {
413 type = SHT_NOBITS;
414 } else if (pass == 1) {
415 nasm_error(ERR_WARNING, "Unknown section attribute '%s' ignored on"
416 " declaration of section `%s'", q, name);
420 if (!strcmp(name, ".shstrtab") ||
421 !strcmp(name, ".symtab") ||
422 !strcmp(name, ".strtab")) {
423 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
424 "name `%s'", name);
425 return NO_SEG;
428 for (i = 0; i < nsects; i++)
429 if (!strcmp(name, sects[i]->name))
430 break;
431 if (i == nsects) {
432 const struct elf_known_section *ks = elf_known_sections;
434 while (ks->name) {
435 if (!strcmp(name, ks->name))
436 break;
437 ks++;
440 type = type ? type : ks->type;
441 align = align ? align : ks->align;
442 flags = (ks->flags & ~flags_and) | flags_or;
444 i = elf_make_section(name, type, flags, align);
445 } else if (pass == 1) {
446 if ((type && sects[i]->type != type)
447 || (align && sects[i]->align != align)
448 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
449 nasm_error(ERR_WARNING, "section attributes ignored on"
450 " redeclaration of section `%s'", name);
453 return sects[i]->index;
456 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
457 int is_global, char *special)
459 int pos = strslen;
460 struct Symbol *sym;
461 bool special_used = false;
463 #if defined(DEBUG) && DEBUG>2
464 nasm_error(ERR_DEBUG,
465 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
466 name, segment, offset, is_global, special);
467 #endif
468 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
470 * This is a NASM special symbol. We never allow it into
471 * the ELF symbol table, even if it's a valid one. If it
472 * _isn't_ a valid one, we should barf immediately.
474 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
475 strcmp(name, "..got") && strcmp(name, "..plt") &&
476 strcmp(name, "..sym") && strcmp(name, "..tlsie"))
477 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
478 return;
481 if (is_global == 3) {
482 struct Symbol **s;
484 * Fix up a forward-reference symbol size from the first
485 * pass.
487 for (s = &fwds; *s; s = &(*s)->nextfwd)
488 if (!strcmp((*s)->name, name)) {
489 struct tokenval tokval;
490 expr *e;
491 char *p = special;
493 while (*p && !nasm_isspace(*p))
494 p++;
495 while (*p && nasm_isspace(*p))
496 p++;
497 stdscan_reset();
498 stdscan_bufptr = p;
499 tokval.t_type = TOKEN_INVALID;
500 e = evaluate(stdscan, NULL, &tokval, NULL, 1, nasm_error, NULL);
501 if (e) {
502 if (!is_simple(e))
503 nasm_error(ERR_NONFATAL, "cannot use relocatable"
504 " expression as symbol size");
505 else
506 (*s)->size = reloc_value(e);
510 * Remove it from the list of unresolved sizes.
512 nasm_free((*s)->name);
513 *s = (*s)->nextfwd;
514 return;
516 return; /* it wasn't an important one */
519 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
520 strslen += 1 + strlen(name);
522 lastsym = sym = saa_wstruct(syms);
524 memset(&sym->symv, 0, sizeof(struct rbtree));
526 sym->strpos = pos;
527 sym->type = is_global ? SYM_GLOBAL : 0;
528 sym->other = STV_DEFAULT;
529 sym->size = 0;
530 if (segment == NO_SEG)
531 sym->section = SHN_ABS;
532 else {
533 int i;
534 sym->section = SHN_UNDEF;
535 if (nsects == 0 && segment == def_seg) {
536 int tempint;
537 if (segment != elf_section_names(".text", 2, &tempint))
538 nasm_error(ERR_PANIC,
539 "strange segment conditions in ELF driver");
540 sym->section = nsects;
541 } else {
542 for (i = 0; i < nsects; i++)
543 if (segment == sects[i]->index) {
544 sym->section = i + 1;
545 break;
550 if (is_global == 2) {
551 sym->size = offset;
552 sym->symv.key = 0;
553 sym->section = SHN_COMMON;
555 * We have a common variable. Check the special text to see
556 * if it's a valid number and power of two; if so, store it
557 * as the alignment for the common variable.
559 if (special) {
560 bool err;
561 sym->symv.key = readnum(special, &err);
562 if (err)
563 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
564 " valid number", special);
565 else if ((sym->symv.key | (sym->symv.key - 1))
566 != 2 * sym->symv.key - 1)
567 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
568 " power of two", special);
570 special_used = true;
571 } else
572 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
574 if (sym->type == SYM_GLOBAL) {
576 * If sym->section == SHN_ABS, then the first line of the
577 * else section would cause a core dump, because its a reference
578 * beyond the end of the section array.
579 * This behaviour is exhibited by this code:
580 * GLOBAL crash_nasm
581 * crash_nasm equ 0
582 * To avoid such a crash, such requests are silently discarded.
583 * This may not be the best solution.
585 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
586 bsym = raa_write(bsym, segment, nglobs);
587 } else if (sym->section != SHN_ABS) {
589 * This is a global symbol; so we must add it to the rbtree
590 * of global symbols in its section.
592 * In addition, we check the special text for symbol
593 * type and size information.
595 sects[sym->section-1]->gsyms =
596 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
598 if (special) {
599 int n = strcspn(special, " \t");
601 if (!nasm_strnicmp(special, "function", n))
602 sym->type |= STT_FUNC;
603 else if (!nasm_strnicmp(special, "data", n) ||
604 !nasm_strnicmp(special, "object", n))
605 sym->type |= STT_OBJECT;
606 else if (!nasm_strnicmp(special, "notype", n))
607 sym->type |= STT_NOTYPE;
608 else
609 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
610 n, special);
611 special += n;
613 while (nasm_isspace(*special))
614 ++special;
615 if (*special) {
616 n = strcspn(special, " \t");
617 if (!nasm_strnicmp(special, "default", n))
618 sym->other = STV_DEFAULT;
619 else if (!nasm_strnicmp(special, "internal", n))
620 sym->other = STV_INTERNAL;
621 else if (!nasm_strnicmp(special, "hidden", n))
622 sym->other = STV_HIDDEN;
623 else if (!nasm_strnicmp(special, "protected", n))
624 sym->other = STV_PROTECTED;
625 else
626 n = 0;
627 special += n;
630 if (*special) {
631 struct tokenval tokval;
632 expr *e;
633 int fwd = 0;
634 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
636 while (special[n] && nasm_isspace(special[n]))
637 n++;
639 * We have a size expression; attempt to
640 * evaluate it.
642 stdscan_reset();
643 stdscan_bufptr = special + n;
644 tokval.t_type = TOKEN_INVALID;
645 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, nasm_error,
646 NULL);
647 if (fwd) {
648 sym->nextfwd = fwds;
649 fwds = sym;
650 sym->name = nasm_strdup(name);
651 } else if (e) {
652 if (!is_simple(e))
653 nasm_error(ERR_NONFATAL, "cannot use relocatable"
654 " expression as symbol size");
655 else
656 sym->size = reloc_value(e);
658 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
660 special_used = true;
663 * If TLS segment, mark symbol accordingly.
665 if (sects[sym->section - 1]->flags & SHF_TLS) {
666 sym->type &= 0xf0;
667 sym->type |= STT_TLS;
670 sym->globnum = nglobs;
671 nglobs++;
672 } else
673 nlocals++;
675 if (special && !special_used)
676 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
679 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
681 struct Reloc *r;
683 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
684 sect->tail = &r->next;
685 r->next = NULL;
687 r->address = sect->len;
688 if (segment == NO_SEG)
689 r->symbol = 0;
690 else {
691 int i;
692 r->symbol = 0;
693 for (i = 0; i < nsects; i++)
694 if (segment == sects[i]->index)
695 r->symbol = i + 2;
696 if (!r->symbol)
697 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
699 r->type = type;
701 sect->nrelocs++;
705 * This routine deals with ..got and ..sym relocations: the more
706 * complicated kinds. In shared-library writing, some relocations
707 * with respect to global symbols must refer to the precise symbol
708 * rather than referring to an offset from the base of the section
709 * _containing_ the symbol. Such relocations call to this routine,
710 * which searches the symbol list for the symbol in question.
712 * R_386_GOT32 references require the _exact_ symbol address to be
713 * used; R_386_32 references can be at an offset from the symbol.
714 * The boolean argument `exact' tells us this.
716 * Return value is the adjusted value of `addr', having become an
717 * offset from the symbol rather than the section. Should always be
718 * zero when returning from an exact call.
720 * Limitation: if you define two symbols at the same place,
721 * confusion will occur.
723 * Inefficiency: we search, currently, using a linked list which
724 * isn't even necessarily sorted.
726 static int32_t elf_add_gsym_reloc(struct Section *sect,
727 int32_t segment, uint32_t offset,
728 int type, bool exact)
730 struct Reloc *r;
731 struct Section *s;
732 struct Symbol *sym;
733 struct rbtree *srb;
734 int i;
737 * First look up the segment/offset pair and find a global
738 * symbol corresponding to it. If it's not one of our segments,
739 * then it must be an external symbol, in which case we're fine
740 * doing a normal elf_add_reloc after first sanity-checking
741 * that the offset from the symbol is zero.
743 s = NULL;
744 for (i = 0; i < nsects; i++)
745 if (segment == sects[i]->index) {
746 s = sects[i];
747 break;
749 if (!s) {
750 if (exact && offset != 0)
751 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
752 " for this reference");
753 else
754 elf_add_reloc(sect, segment, type);
755 return offset;
758 srb = rb_search(s->gsyms, offset);
759 if (!srb || (exact && srb->key != offset)) {
760 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
761 " for this reference");
762 return 0;
764 sym = container_of(srb, struct Symbol, symv);
766 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
767 sect->tail = &r->next;
768 r->next = NULL;
770 r->address = sect->len;
771 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
772 r->type = type;
774 sect->nrelocs++;
776 return offset - sym->symv.key;
779 static void elf_out(int32_t segto, const void *data,
780 enum out_type type, uint64_t size,
781 int32_t segment, int32_t wrt)
783 struct Section *s;
784 int32_t addr;
785 uint8_t mydata[4], *p;
786 int i;
787 static struct symlininfo sinfo;
790 * handle absolute-assembly (structure definitions)
792 if (segto == NO_SEG) {
793 if (type != OUT_RESERVE)
794 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
795 " space");
796 return;
799 s = NULL;
800 for (i = 0; i < nsects; i++)
801 if (segto == sects[i]->index) {
802 s = sects[i];
803 break;
805 if (!s) {
806 int tempint; /* ignored */
807 if (segto != elf_section_names(".text", 2, &tempint))
808 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
809 else {
810 s = sects[nsects - 1];
811 i = nsects - 1;
815 /* again some stabs debugging stuff */
816 if (of_elf32.current_dfmt) {
817 sinfo.offset = s->len;
818 sinfo.section = i;
819 sinfo.name = s->name;
820 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
822 /* end of debugging stuff */
824 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
825 nasm_error(ERR_WARNING, "attempt to initialize memory in"
826 " BSS section `%s': ignored", s->name);
827 s->len += realsize(type, size);
828 return;
831 if (type == OUT_RESERVE) {
832 if (s->type == SHT_PROGBITS) {
833 nasm_error(ERR_WARNING, "uninitialized space declared in"
834 " non-BSS section `%s': zeroing", s->name);
835 elf_sect_write(s, NULL, size);
836 } else
837 s->len += size;
838 } else if (type == OUT_RAWDATA) {
839 if (segment != NO_SEG)
840 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
841 elf_sect_write(s, data, size);
842 } else if (type == OUT_ADDRESS) {
843 bool gnu16 = false;
844 addr = *(int64_t *)data;
845 if (segment != NO_SEG) {
846 if (segment % 2) {
847 nasm_error(ERR_NONFATAL, "ELF format does not support"
848 " segment base references");
849 } else {
850 if (wrt == NO_SEG) {
851 if (size == 2) {
852 gnu16 = true;
853 elf_add_reloc(s, segment, R_386_16);
854 } else {
855 elf_add_reloc(s, segment, R_386_32);
857 } else if (wrt == elf_gotpc_sect + 1) {
859 * The user will supply GOT relative to $$. ELF
860 * will let us have GOT relative to $. So we
861 * need to fix up the data item by $-$$.
863 addr += s->len;
864 elf_add_reloc(s, segment, R_386_GOTPC);
865 } else if (wrt == elf_gotoff_sect + 1) {
866 elf_add_reloc(s, segment, R_386_GOTOFF);
867 } else if (wrt == elf_tlsie_sect + 1) {
868 addr = elf_add_gsym_reloc(s, segment, addr,
869 R_386_TLS_IE, true);
870 } else if (wrt == elf_got_sect + 1) {
871 addr = elf_add_gsym_reloc(s, segment, addr,
872 R_386_GOT32, true);
873 } else if (wrt == elf_sym_sect + 1) {
874 if (size == 2) {
875 gnu16 = true;
876 addr = elf_add_gsym_reloc(s, segment, addr,
877 R_386_16, false);
878 } else {
879 addr = elf_add_gsym_reloc(s, segment, addr,
880 R_386_32, false);
882 } else if (wrt == elf_plt_sect + 1) {
883 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
884 "relative PLT references");
885 } else {
886 nasm_error(ERR_NONFATAL, "ELF format does not support this"
887 " use of WRT");
888 wrt = NO_SEG; /* we can at least _try_ to continue */
892 p = mydata;
893 if (gnu16) {
894 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
895 "16-bit relocations in ELF is a GNU extension");
896 WRITESHORT(p, addr);
897 } else {
898 if (size != 4 && segment != NO_SEG) {
899 nasm_error(ERR_NONFATAL,
900 "Unsupported non-32-bit ELF relocation");
902 WRITELONG(p, addr);
904 elf_sect_write(s, mydata, size);
905 } else if (type == OUT_REL2ADR) {
906 if (segment == segto)
907 nasm_error(ERR_PANIC, "intra-segment OUT_REL2ADR");
908 if (segment != NO_SEG && segment % 2) {
909 nasm_error(ERR_NONFATAL, "ELF format does not support"
910 " segment base references");
911 } else {
912 if (wrt == NO_SEG) {
913 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
914 "16-bit relocations in ELF is a GNU extension");
915 elf_add_reloc(s, segment, R_386_PC16);
916 } else {
917 nasm_error(ERR_NONFATAL,
918 "Unsupported non-32-bit ELF relocation");
921 p = mydata;
922 WRITESHORT(p, *(int64_t *)data - size);
923 elf_sect_write(s, mydata, 2L);
924 } else if (type == OUT_REL4ADR) {
925 if (segment == segto)
926 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
927 if (segment != NO_SEG && segment % 2) {
928 nasm_error(ERR_NONFATAL, "ELF format does not support"
929 " segment base references");
930 } else {
931 if (wrt == NO_SEG) {
932 elf_add_reloc(s, segment, R_386_PC32);
933 } else if (wrt == elf_plt_sect + 1) {
934 elf_add_reloc(s, segment, R_386_PLT32);
935 } else if (wrt == elf_gotpc_sect + 1 ||
936 wrt == elf_gotoff_sect + 1 ||
937 wrt == elf_got_sect + 1) {
938 nasm_error(ERR_NONFATAL, "ELF format cannot produce PC-"
939 "relative GOT references");
940 } else {
941 nasm_error(ERR_NONFATAL, "ELF format does not support this"
942 " use of WRT");
943 wrt = NO_SEG; /* we can at least _try_ to continue */
946 p = mydata;
947 WRITELONG(p, *(int64_t *)data - size);
948 elf_sect_write(s, mydata, 4L);
952 static void elf_write(void)
954 int align;
955 char *p;
956 int i;
958 struct SAA *symtab;
959 int32_t symtablen, symtablocal;
962 * Work out how many sections we will have. We have SHN_UNDEF,
963 * then the flexible user sections, then the fixed sections
964 * `.shstrtab', `.symtab' and `.strtab', then optionally
965 * relocation sections for the user sections.
967 nsections = sec_numspecial + 1;
968 if (of_elf32.current_dfmt == &df_stabs)
969 nsections += 3;
970 else if (of_elf32.current_dfmt == &df_dwarf)
971 nsections += 10;
973 add_sectname("", ".shstrtab");
974 add_sectname("", ".symtab");
975 add_sectname("", ".strtab");
976 for (i = 0; i < nsects; i++) {
977 nsections++; /* for the section itself */
978 if (sects[i]->head) {
979 nsections++; /* for its relocations */
980 add_sectname(".rel", sects[i]->name);
984 if (of_elf32.current_dfmt == &df_stabs) {
985 /* in case the debug information is wanted, just add these three sections... */
986 add_sectname("", ".stab");
987 add_sectname("", ".stabstr");
988 add_sectname(".rel", ".stab");
989 } else if (of_elf32.current_dfmt == &df_dwarf) {
990 /* the dwarf debug standard specifies the following ten sections,
991 not all of which are currently implemented,
992 although all of them are defined. */
993 add_sectname("", ".debug_aranges");
994 add_sectname(".rela", ".debug_aranges");
995 add_sectname("", ".debug_pubnames");
996 add_sectname("", ".debug_info");
997 add_sectname(".rela", ".debug_info");
998 add_sectname("", ".debug_abbrev");
999 add_sectname("", ".debug_line");
1000 add_sectname(".rela", ".debug_line");
1001 add_sectname("", ".debug_frame");
1002 add_sectname("", ".debug_loc");
1006 * Output the ELF header.
1008 fwrite("\177ELF\1\1\1", 7, 1, ofile);
1009 fputc(elf_osabi, ofile);
1010 fputc(elf_abiver, ofile);
1011 fwritezero(7, ofile);
1012 fwriteint16_t(1, ofile); /* ET_REL relocatable file */
1013 fwriteint16_t(3, ofile); /* EM_386 processor ID */
1014 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
1015 fwriteint32_t(0L, ofile); /* no entry point */
1016 fwriteint32_t(0L, ofile); /* no program header table */
1017 fwriteint32_t(0x40L, ofile); /* section headers straight after
1018 * ELF header plus alignment */
1019 fwriteint32_t(0L, ofile); /* 386 defines no special flags */
1020 fwriteint16_t(0x34, ofile); /* size of ELF header */
1021 fwriteint16_t(0, ofile); /* no program header table, again */
1022 fwriteint16_t(0, ofile); /* still no program header table */
1023 fwriteint16_t(0x28, ofile); /* size of section header */
1024 fwriteint16_t(nsections, ofile); /* number of sections */
1025 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
1026 * section header table */
1027 fwriteint32_t(0L, ofile); /* align to 0x40 bytes */
1028 fwriteint32_t(0L, ofile);
1029 fwriteint32_t(0L, ofile);
1032 * Build the symbol table and relocation tables.
1034 symtab = elf_build_symtab(&symtablen, &symtablocal);
1035 for (i = 0; i < nsects; i++)
1036 if (sects[i]->head)
1037 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1038 sects[i]->head);
1041 * Now output the section header table.
1044 elf_foffs = 0x40 + 0x28 * nsections;
1045 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1046 elf_foffs += align;
1047 elf_nsect = 0;
1048 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1050 /* SHN_UNDEF */
1051 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1052 p = shstrtab + 1;
1054 /* The normal sections */
1055 for (i = 0; i < nsects; i++) {
1056 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1057 (sects[i]->type == SHT_PROGBITS ?
1058 sects[i]->data : NULL), true,
1059 sects[i]->len, 0, 0, sects[i]->align, 0);
1060 p += strlen(p) + 1;
1063 /* .shstrtab */
1064 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1065 shstrtablen, 0, 0, 1, 0);
1066 p += strlen(p) + 1;
1068 /* .symtab */
1069 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1070 symtablen, sec_strtab, symtablocal, 4, 16);
1071 p += strlen(p) + 1;
1073 /* .strtab */
1074 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1075 strslen, 0, 0, 1, 0);
1076 p += strlen(p) + 1;
1078 /* The relocation sections */
1079 for (i = 0; i < nsects; i++)
1080 if (sects[i]->head) {
1081 elf_section_header(p - shstrtab, SHT_REL, 0, sects[i]->rel, true,
1082 sects[i]->rellen, sec_symtab, i + 1, 4, 8);
1083 p += strlen(p) + 1;
1087 if (of_elf32.current_dfmt == &df_stabs) {
1088 /* for debugging information, create the last three sections
1089 which are the .stab , .stabstr and .rel.stab sections respectively */
1091 /* this function call creates the stab sections in memory */
1092 stabs32_generate();
1094 if (stabbuf && stabstrbuf && stabrelbuf) {
1095 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1096 stablen, sec_stabstr, 0, 4, 12);
1097 p += strlen(p) + 1;
1099 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1100 stabstrlen, 0, 0, 4, 0);
1101 p += strlen(p) + 1;
1103 /* link -> symtable info -> section to refer to */
1104 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1105 stabrellen, sec_symtab, sec_stab, 4, 8);
1106 p += strlen(p) + 1;
1108 } else if (of_elf32.current_dfmt == &df_dwarf) {
1109 /* for dwarf debugging information, create the ten dwarf sections */
1111 /* this function call creates the dwarf sections in memory */
1112 if (dwarf_fsect)
1113 dwarf32_generate();
1115 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1116 arangeslen, 0, 0, 1, 0);
1117 p += strlen(p) + 1;
1119 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1120 arangesrellen, sec_symtab, sec_debug_aranges,
1121 1, 12);
1122 p += strlen(p) + 1;
1124 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf,
1125 false, pubnameslen, 0, 0, 1, 0);
1126 p += strlen(p) + 1;
1128 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1129 infolen, 0, 0, 1, 0);
1130 p += strlen(p) + 1;
1132 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1133 inforellen, sec_symtab, sec_debug_info, 1, 12);
1134 p += strlen(p) + 1;
1136 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1137 abbrevlen, 0, 0, 1, 0);
1138 p += strlen(p) + 1;
1140 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1141 linelen, 0, 0, 1, 0);
1142 p += strlen(p) + 1;
1144 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1145 linerellen, sec_symtab, sec_debug_line, 1, 12);
1146 p += strlen(p) + 1;
1148 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1149 framelen, 0, 0, 8, 0);
1150 p += strlen(p) + 1;
1152 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1153 loclen, 0, 0, 1, 0);
1154 p += strlen(p) + 1;
1156 fwritezero(align, ofile);
1159 * Now output the sections.
1161 elf_write_sections();
1163 nasm_free(elf_sects);
1164 saa_free(symtab);
1167 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1169 struct SAA *s = saa_init(1L);
1170 struct Symbol *sym;
1171 uint8_t entry[16], *p;
1172 int i;
1174 *len = *local = 0;
1177 * First, an all-zeros entry, required by the ELF spec.
1179 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1180 *len += 16;
1181 (*local)++;
1184 * Next, an entry for the file name.
1186 p = entry;
1187 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1188 WRITELONG(p, 0); /* no value */
1189 WRITELONG(p, 0); /* no size either */
1190 WRITESHORT(p, STT_FILE); /* type FILE */
1191 WRITESHORT(p, SHN_ABS);
1192 saa_wbytes(s, entry, 16L);
1193 *len += 16;
1194 (*local)++;
1197 * Now some standard symbols defining the segments, for relocation
1198 * purposes.
1200 for (i = 1; i <= nsects; i++) {
1201 p = entry;
1202 WRITELONG(p, 0); /* no symbol name */
1203 WRITELONG(p, 0); /* offset zero */
1204 WRITELONG(p, 0); /* size zero */
1205 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1206 WRITESHORT(p, i); /* section id */
1207 saa_wbytes(s, entry, 16L);
1208 *len += 16;
1209 (*local)++;
1213 * Now the other local symbols.
1215 saa_rewind(syms);
1216 while ((sym = saa_rstruct(syms))) {
1217 if (sym->type & SYM_GLOBAL)
1218 continue;
1219 p = entry;
1220 WRITELONG(p, sym->strpos);
1221 WRITELONG(p, sym->symv.key);
1222 WRITELONG(p, sym->size);
1223 WRITECHAR(p, sym->type); /* type and binding */
1224 WRITECHAR(p, sym->other); /* visibility */
1225 WRITESHORT(p, sym->section);
1226 saa_wbytes(s, entry, 16L);
1227 *len += 16;
1228 (*local)++;
1231 * dwarf needs symbols for debug sections
1232 * which are relocation targets.
1234 //*** fix for 32 bit
1235 if (of_elf32.current_dfmt == &df_dwarf) {
1236 dwarf_infosym = *local;
1237 p = entry;
1238 WRITELONG(p, 0); /* no symbol name */
1239 WRITELONG(p, (uint32_t) 0); /* offset zero */
1240 WRITELONG(p, (uint32_t) 0); /* size zero */
1241 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1242 WRITESHORT(p, sec_debug_info); /* section id */
1243 saa_wbytes(s, entry, 16L);
1244 *len += 16;
1245 (*local)++;
1246 dwarf_abbrevsym = *local;
1247 p = entry;
1248 WRITELONG(p, 0); /* no symbol name */
1249 WRITELONG(p, (uint32_t) 0); /* offset zero */
1250 WRITELONG(p, (uint32_t) 0); /* size zero */
1251 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1252 WRITESHORT(p, sec_debug_abbrev); /* section id */
1253 saa_wbytes(s, entry, 16L);
1254 *len += 16;
1255 (*local)++;
1256 dwarf_linesym = *local;
1257 p = entry;
1258 WRITELONG(p, 0); /* no symbol name */
1259 WRITELONG(p, (uint32_t) 0); /* offset zero */
1260 WRITELONG(p, (uint32_t) 0); /* size zero */
1261 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1262 WRITESHORT(p, sec_debug_line); /* section id */
1263 saa_wbytes(s, entry, 16L);
1264 *len += 16;
1265 (*local)++;
1269 * Now the global symbols.
1271 saa_rewind(syms);
1272 while ((sym = saa_rstruct(syms))) {
1273 if (!(sym->type & SYM_GLOBAL))
1274 continue;
1275 p = entry;
1276 WRITELONG(p, sym->strpos);
1277 WRITELONG(p, sym->symv.key);
1278 WRITELONG(p, sym->size);
1279 WRITECHAR(p, sym->type); /* type and binding */
1280 WRITECHAR(p, sym->other); /* visibility */
1281 WRITESHORT(p, sym->section);
1282 saa_wbytes(s, entry, 16L);
1283 *len += 16;
1286 return s;
1289 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1291 struct SAA *s;
1292 uint8_t *p, entry[8];
1293 int32_t global_offset;
1295 if (!r)
1296 return NULL;
1298 s = saa_init(1L);
1299 *len = 0;
1302 * How to onvert from a global placeholder to a real symbol index;
1303 * the +2 refers to the two special entries, the null entry and
1304 * the filename entry.
1306 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1308 while (r) {
1309 int32_t sym = r->symbol;
1312 * Create a real symbol index; the +2 refers to the two special
1313 * entries, the null entry and the filename entry.
1315 if (sym >= GLOBAL_TEMP_BASE)
1316 sym += global_offset;
1318 p = entry;
1319 WRITELONG(p, r->address);
1320 WRITELONG(p, (sym << 8) + r->type);
1321 saa_wbytes(s, entry, 8L);
1322 *len += 8;
1324 r = r->next;
1327 return s;
1330 static void elf_section_header(int name, int type, int flags,
1331 void *data, bool is_saa, int32_t datalen,
1332 int link, int info, int align, int eltsize)
1334 elf_sects[elf_nsect].data = data;
1335 elf_sects[elf_nsect].len = datalen;
1336 elf_sects[elf_nsect].is_saa = is_saa;
1337 elf_nsect++;
1339 fwriteint32_t((int32_t)name, ofile);
1340 fwriteint32_t((int32_t)type, ofile);
1341 fwriteint32_t((int32_t)flags, ofile);
1342 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
1343 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
1344 fwriteint32_t(datalen, ofile);
1345 if (data)
1346 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1347 fwriteint32_t((int32_t)link, ofile);
1348 fwriteint32_t((int32_t)info, ofile);
1349 fwriteint32_t((int32_t)align, ofile);
1350 fwriteint32_t((int32_t)eltsize, ofile);
1353 static void elf_write_sections(void)
1355 int i;
1356 for (i = 0; i < elf_nsect; i++)
1357 if (elf_sects[i].data) {
1358 int32_t len = elf_sects[i].len;
1359 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1360 int32_t align = reallen - len;
1361 if (elf_sects[i].is_saa)
1362 saa_fpwrite(elf_sects[i].data, ofile);
1363 else
1364 fwrite(elf_sects[i].data, len, 1, ofile);
1365 fwritezero(align, ofile);
1369 static void elf_sect_write(struct Section *sect,
1370 const uint8_t *data, uint32_t len)
1372 saa_wbytes(sect->data, data, len);
1373 sect->len += len;
1376 static int32_t elf_segbase(int32_t segment)
1378 return segment;
1381 static int elf_directive(enum directives directive, char *value, int pass)
1383 bool err;
1384 int64_t n;
1385 char *p;
1387 switch (directive) {
1388 case D_OSABI:
1389 if (pass == 2)
1390 return 1; /* ignore in pass 2 */
1392 n = readnum(value, &err);
1393 if (err) {
1394 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1395 return 1;
1397 if (n < 0 || n > 255) {
1398 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1399 return 1;
1401 elf_osabi = n;
1402 elf_abiver = 0;
1404 if ((p = strchr(value,',')) == NULL)
1405 return 1;
1407 n = readnum(p+1, &err);
1408 if (err || n < 0 || n > 255) {
1409 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1410 return 1;
1413 elf_abiver = n;
1414 return 1;
1416 default:
1417 return 0;
1421 static void elf_filename(char *inname, char *outname)
1423 strcpy(elf_module, inname);
1424 standard_extension(inname, outname, ".o");
1427 extern macros_t elf_stdmac[];
1429 static int elf_set_info(enum geninfo type, char **val)
1431 (void)type;
1432 (void)val;
1433 return 0;
1435 static struct dfmt df_dwarf = {
1436 "ELF32 (i386) dwarf debug format for Linux/Unix",
1437 "dwarf",
1438 dwarf32_init,
1439 dwarf32_linenum,
1440 debug32_deflabel,
1441 debug32_directive,
1442 debug32_typevalue,
1443 dwarf32_output,
1444 dwarf32_cleanup
1446 static struct dfmt df_stabs = {
1447 "ELF32 (i386) stabs debug format for Linux/Unix",
1448 "stabs",
1449 null_debug_init,
1450 stabs32_linenum,
1451 debug32_deflabel,
1452 debug32_directive,
1453 debug32_typevalue,
1454 stabs32_output,
1455 stabs32_cleanup
1458 struct dfmt *elf32_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1460 struct ofmt of_elf32 = {
1461 "ELF32 (i386) object files (e.g. Linux)",
1462 "elf32",
1464 elf32_debugs_arr,
1465 &df_stabs,
1466 elf_stdmac,
1467 elf_init,
1468 elf_set_info,
1469 elf_out,
1470 elf_deflabel,
1471 elf_section_names,
1472 elf_segbase,
1473 elf_directive,
1474 elf_filename,
1475 elf_cleanup
1478 struct ofmt of_elf = {
1479 "ELF (short name for ELF32) ",
1480 "elf",
1482 elf32_debugs_arr,
1483 &df_stabs,
1484 elf_stdmac,
1485 elf_init_hack,
1486 elf_set_info,
1487 elf_out,
1488 elf_deflabel,
1489 elf_section_names,
1490 elf_segbase,
1491 elf_directive,
1492 elf_filename,
1493 elf_cleanup
1495 /* again, the stabs debugging stuff (code) */
1497 static void stabs32_linenum(const char *filename, int32_t linenumber,
1498 int32_t segto)
1500 (void)segto;
1502 if (!stabs_filename) {
1503 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1504 strcpy(stabs_filename, filename);
1505 } else {
1506 if (strcmp(stabs_filename, filename)) {
1507 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1508 in fact, this leak comes in quite handy to maintain a list of files
1509 encountered so far in the symbol lines... */
1511 /* why not nasm_free(stabs_filename); we're done with the old one */
1513 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1514 strcpy(stabs_filename, filename);
1517 debug_immcall = 1;
1518 currentline = linenumber;
1521 static void debug32_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1522 char *special)
1524 (void)name;
1525 (void)segment;
1526 (void)offset;
1527 (void)is_global;
1528 (void)special;
1531 static void debug32_directive(const char *directive, const char *params)
1533 (void)directive;
1534 (void)params;
1537 static void debug32_typevalue(int32_t type)
1539 int32_t stype, ssize;
1540 switch (TYM_TYPE(type)) {
1541 case TY_LABEL:
1542 ssize = 0;
1543 stype = STT_NOTYPE;
1544 break;
1545 case TY_BYTE:
1546 ssize = 1;
1547 stype = STT_OBJECT;
1548 break;
1549 case TY_WORD:
1550 ssize = 2;
1551 stype = STT_OBJECT;
1552 break;
1553 case TY_DWORD:
1554 ssize = 4;
1555 stype = STT_OBJECT;
1556 break;
1557 case TY_FLOAT:
1558 ssize = 4;
1559 stype = STT_OBJECT;
1560 break;
1561 case TY_QWORD:
1562 ssize = 8;
1563 stype = STT_OBJECT;
1564 break;
1565 case TY_TBYTE:
1566 ssize = 10;
1567 stype = STT_OBJECT;
1568 break;
1569 case TY_OWORD:
1570 ssize = 16;
1571 stype = STT_OBJECT;
1572 break;
1573 case TY_YWORD:
1574 ssize = 32;
1575 stype = STT_OBJECT;
1576 break;
1577 case TY_COMMON:
1578 ssize = 0;
1579 stype = STT_COMMON;
1580 break;
1581 case TY_SEG:
1582 ssize = 0;
1583 stype = STT_SECTION;
1584 break;
1585 case TY_EXTERN:
1586 ssize = 0;
1587 stype = STT_NOTYPE;
1588 break;
1589 case TY_EQU:
1590 ssize = 0;
1591 stype = STT_NOTYPE;
1592 break;
1593 default:
1594 ssize = 0;
1595 stype = STT_NOTYPE;
1596 break;
1598 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1599 lastsym->size = ssize;
1600 lastsym->type = stype;
1604 static void stabs32_output(int type, void *param)
1606 struct symlininfo *s;
1607 struct linelist *el;
1608 if (type == TY_STABSSYMLIN) {
1609 if (debug_immcall) {
1610 s = (struct symlininfo *)param;
1611 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1612 return; /* we are only interested in the text stuff */
1613 numlinestabs++;
1614 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1615 el->info.offset = s->offset;
1616 el->info.section = s->section;
1617 el->info.name = s->name;
1618 el->line = currentline;
1619 el->filename = stabs_filename;
1620 el->next = 0;
1621 if (stabslines) {
1622 stabslines->last->next = el;
1623 stabslines->last = el;
1624 } else {
1625 stabslines = el;
1626 stabslines->last = el;
1630 debug_immcall = 0;
1633 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1634 do {\
1635 WRITELONG(p,n_strx); \
1636 WRITECHAR(p,n_type); \
1637 WRITECHAR(p,n_other); \
1638 WRITESHORT(p,n_desc); \
1639 WRITELONG(p,n_value); \
1640 } while (0)
1642 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1644 static void stabs32_generate(void)
1646 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1647 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1648 char **allfiles;
1649 int *fileidx;
1651 struct linelist *ptr;
1653 ptr = stabslines;
1655 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1656 for (i = 0; i < numlinestabs; i++)
1657 allfiles[i] = 0;
1658 numfiles = 0;
1659 while (ptr) {
1660 if (numfiles == 0) {
1661 allfiles[0] = ptr->filename;
1662 numfiles++;
1663 } else {
1664 for (i = 0; i < numfiles; i++) {
1665 if (!strcmp(allfiles[i], ptr->filename))
1666 break;
1668 if (i >= numfiles) {
1669 allfiles[i] = ptr->filename;
1670 numfiles++;
1673 ptr = ptr->next;
1675 strsize = 1;
1676 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1677 for (i = 0; i < numfiles; i++) {
1678 fileidx[i] = strsize;
1679 strsize += strlen(allfiles[i]) + 1;
1681 mainfileindex = 0;
1682 for (i = 0; i < numfiles; i++) {
1683 if (!strcmp(allfiles[i], elf_module)) {
1684 mainfileindex = i;
1685 break;
1689 /* worst case size of the stab buffer would be:
1690 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1692 sbuf =
1693 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1694 sizeof(struct stabentry));
1696 ssbuf = (uint8_t *)nasm_malloc(strsize);
1698 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1699 rptr = rbuf;
1701 for (i = 0; i < numfiles; i++) {
1702 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1704 ssbuf[0] = 0;
1706 stabstrlen = strsize; /* set global variable for length of stab strings */
1708 sptr = sbuf;
1709 ptr = stabslines;
1710 numstabs = 0;
1712 if (ptr) {
1713 /* this is the first stab, its strx points to the filename of the
1714 the source-file, the n_desc field should be set to the number
1715 of remaining stabs
1717 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1719 /* this is the stab for the main source file */
1720 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1722 /* relocation table entry */
1724 /* Since the symbol table has two entries before */
1725 /* the section symbols, the index in the info.section */
1726 /* member must be adjusted by adding 2 */
1728 WRITELONG(rptr, (sptr - sbuf) - 4);
1729 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1731 numstabs++;
1732 currfile = mainfileindex;
1735 while (ptr) {
1736 if (strcmp(allfiles[currfile], ptr->filename)) {
1737 /* oops file has changed... */
1738 for (i = 0; i < numfiles; i++)
1739 if (!strcmp(allfiles[i], ptr->filename))
1740 break;
1741 currfile = i;
1742 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1743 ptr->info.offset);
1744 numstabs++;
1746 /* relocation table entry */
1747 WRITELONG(rptr, (sptr - sbuf) - 4);
1748 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1751 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1752 numstabs++;
1754 /* relocation table entry */
1756 WRITELONG(rptr, (sptr - sbuf) - 4);
1757 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1759 ptr = ptr->next;
1763 ((struct stabentry *)sbuf)->n_desc = numstabs;
1765 nasm_free(allfiles);
1766 nasm_free(fileidx);
1768 stablen = (sptr - sbuf);
1769 stabrellen = (rptr - rbuf);
1770 stabrelbuf = rbuf;
1771 stabbuf = sbuf;
1772 stabstrbuf = ssbuf;
1775 static void stabs32_cleanup(void)
1777 struct linelist *ptr, *del;
1778 if (!stabslines)
1779 return;
1780 ptr = stabslines;
1781 while (ptr) {
1782 del = ptr;
1783 ptr = ptr->next;
1784 nasm_free(del);
1786 if (stabbuf)
1787 nasm_free(stabbuf);
1788 if (stabrelbuf)
1789 nasm_free(stabrelbuf);
1790 if (stabstrbuf)
1791 nasm_free(stabstrbuf);
1794 /* dwarf routines */
1796 static void dwarf32_init(void)
1798 ndebugs = 3; /* 3 debug symbols */
1801 static void dwarf32_linenum(const char *filename, int32_t linenumber,
1802 int32_t segto)
1804 (void)segto;
1805 dwarf32_findfile(filename);
1806 debug_immcall = 1;
1807 currentline = linenumber;
1810 /* called from elf_out with type == TY_DEBUGSYMLIN */
1811 static void dwarf32_output(int type, void *param)
1813 int ln, aa, inx, maxln, soc;
1814 struct symlininfo *s;
1815 struct SAA *plinep;
1817 (void)type;
1819 s = (struct symlininfo *)param;
1820 /* line number info is only gathered for executable sections */
1821 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1822 return;
1823 /* Check if section index has changed */
1824 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1826 dwarf32_findsect(s->section);
1828 /* do nothing unless line or file has changed */
1829 if (debug_immcall)
1831 ln = currentline - dwarf_csect->line;
1832 aa = s->offset - dwarf_csect->offset;
1833 inx = dwarf_clist->line;
1834 plinep = dwarf_csect->psaa;
1835 /* check for file change */
1836 if (!(inx == dwarf_csect->file))
1838 saa_write8(plinep,DW_LNS_set_file);
1839 saa_write8(plinep,inx);
1840 dwarf_csect->file = inx;
1842 /* check for line change */
1843 if (ln)
1845 /* test if in range of special op code */
1846 maxln = line_base + line_range;
1847 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1848 if (ln >= line_base && ln < maxln && soc < 256)
1850 saa_write8(plinep,soc);
1852 else
1854 if (ln)
1856 saa_write8(plinep,DW_LNS_advance_line);
1857 saa_wleb128s(plinep,ln);
1859 if (aa)
1861 saa_write8(plinep,DW_LNS_advance_pc);
1862 saa_wleb128u(plinep,aa);
1865 dwarf_csect->line = currentline;
1866 dwarf_csect->offset = s->offset;
1868 /* show change handled */
1869 debug_immcall = 0;
1874 static void dwarf32_generate(void)
1876 uint8_t *pbuf;
1877 int indx;
1878 struct linelist *ftentry;
1879 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1880 struct SAA *parangesrel, *plinesrel, *pinforel;
1881 struct sectlist *psect;
1882 size_t saalen, linepoff, totlen, highaddr;
1884 /* write epilogues for each line program range */
1885 /* and build aranges section */
1886 paranges = saa_init(1L);
1887 parangesrel = saa_init(1L);
1888 saa_write16(paranges,2); /* dwarf version */
1889 saa_write32(parangesrel, paranges->datalen+4);
1890 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
1891 saa_write32(parangesrel, 0);
1892 saa_write32(paranges,0); /* offset into info */
1893 saa_write8(paranges,4); /* pointer size */
1894 saa_write8(paranges,0); /* not segmented */
1895 saa_write32(paranges,0); /* padding */
1896 /* iterate though sectlist entries */
1897 psect = dwarf_fsect;
1898 totlen = 0;
1899 highaddr = 0;
1900 for (indx = 0; indx < dwarf_nsections; indx++)
1902 plinep = psect->psaa;
1903 /* Line Number Program Epilogue */
1904 saa_write8(plinep,2); /* std op 2 */
1905 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1906 saa_write8(plinep,DW_LNS_extended_op);
1907 saa_write8(plinep,1); /* operand length */
1908 saa_write8(plinep,DW_LNE_end_sequence);
1909 totlen += plinep->datalen;
1910 /* range table relocation entry */
1911 saa_write32(parangesrel, paranges->datalen + 4);
1912 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
1913 saa_write32(parangesrel, (uint32_t) 0);
1914 /* range table entry */
1915 saa_write32(paranges,0x0000); /* range start */
1916 saa_write32(paranges,sects[psect->section]->len); /* range length */
1917 highaddr += sects[psect->section]->len;
1918 /* done with this entry */
1919 psect = psect->next;
1921 saa_write32(paranges,0); /* null address */
1922 saa_write32(paranges,0); /* null length */
1923 saalen = paranges->datalen;
1924 arangeslen = saalen + 4;
1925 arangesbuf = pbuf = nasm_malloc(arangeslen);
1926 WRITELONG(pbuf,saalen); /* initial length */
1927 saa_rnbytes(paranges, pbuf, saalen);
1928 saa_free(paranges);
1930 /* build rela.aranges section */
1931 arangesrellen = saalen = parangesrel->datalen;
1932 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1933 saa_rnbytes(parangesrel, pbuf, saalen);
1934 saa_free(parangesrel);
1936 /* build pubnames section */
1937 ppubnames = saa_init(1L);
1938 saa_write16(ppubnames,3); /* dwarf version */
1939 saa_write32(ppubnames,0); /* offset into info */
1940 saa_write32(ppubnames,0); /* space used in info */
1941 saa_write32(ppubnames,0); /* end of list */
1942 saalen = ppubnames->datalen;
1943 pubnameslen = saalen + 4;
1944 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1945 WRITELONG(pbuf,saalen); /* initial length */
1946 saa_rnbytes(ppubnames, pbuf, saalen);
1947 saa_free(ppubnames);
1949 /* build info section */
1950 pinfo = saa_init(1L);
1951 pinforel = saa_init(1L);
1952 saa_write16(pinfo,2); /* dwarf version */
1953 saa_write32(pinforel, pinfo->datalen + 4);
1954 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
1955 saa_write32(pinforel, 0);
1956 saa_write32(pinfo,0); /* offset into abbrev */
1957 saa_write8(pinfo,4); /* pointer size */
1958 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1959 saa_write32(pinforel, pinfo->datalen + 4);
1960 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1961 saa_write32(pinforel, 0);
1962 saa_write32(pinfo,0); /* DW_AT_low_pc */
1963 saa_write32(pinforel, pinfo->datalen + 4);
1964 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1965 saa_write32(pinforel, 0);
1966 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
1967 saa_write32(pinforel, pinfo->datalen + 4);
1968 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
1969 saa_write32(pinforel, 0);
1970 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1971 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1972 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1973 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1974 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1975 saa_write32(pinforel, pinfo->datalen + 4);
1976 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
1977 saa_write32(pinforel, 0);
1978 saa_write32(pinfo,0); /* DW_AT_low_pc */
1979 saa_write32(pinfo,0); /* DW_AT_frame_base */
1980 saa_write8(pinfo,0); /* end of entries */
1981 saalen = pinfo->datalen;
1982 infolen = saalen + 4;
1983 infobuf = pbuf = nasm_malloc(infolen);
1984 WRITELONG(pbuf,saalen); /* initial length */
1985 saa_rnbytes(pinfo, pbuf, saalen);
1986 saa_free(pinfo);
1988 /* build rela.info section */
1989 inforellen = saalen = pinforel->datalen;
1990 inforelbuf = pbuf = nasm_malloc(inforellen);
1991 saa_rnbytes(pinforel, pbuf, saalen);
1992 saa_free(pinforel);
1994 /* build abbrev section */
1995 pabbrev = saa_init(1L);
1996 saa_write8(pabbrev,1); /* entry number LEB128u */
1997 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
1998 saa_write8(pabbrev,1); /* has children */
1999 /* the following attributes and forms are all LEB128u values */
2000 saa_write8(pabbrev,DW_AT_low_pc);
2001 saa_write8(pabbrev,DW_FORM_addr);
2002 saa_write8(pabbrev,DW_AT_high_pc);
2003 saa_write8(pabbrev,DW_FORM_addr);
2004 saa_write8(pabbrev,DW_AT_stmt_list);
2005 saa_write8(pabbrev,DW_FORM_data4);
2006 saa_write8(pabbrev,DW_AT_name);
2007 saa_write8(pabbrev,DW_FORM_string);
2008 saa_write8(pabbrev,DW_AT_producer);
2009 saa_write8(pabbrev,DW_FORM_string);
2010 saa_write8(pabbrev,DW_AT_language);
2011 saa_write8(pabbrev,DW_FORM_data2);
2012 saa_write16(pabbrev,0); /* end of entry */
2013 /* LEB128u usage same as above */
2014 saa_write8(pabbrev,2); /* entry number */
2015 saa_write8(pabbrev,DW_TAG_subprogram);
2016 saa_write8(pabbrev,0); /* no children */
2017 saa_write8(pabbrev,DW_AT_low_pc);
2018 saa_write8(pabbrev,DW_FORM_addr);
2019 saa_write8(pabbrev,DW_AT_frame_base);
2020 saa_write8(pabbrev,DW_FORM_data4);
2021 saa_write16(pabbrev,0); /* end of entry */
2022 abbrevlen = saalen = pabbrev->datalen;
2023 abbrevbuf = pbuf = nasm_malloc(saalen);
2024 saa_rnbytes(pabbrev, pbuf, saalen);
2025 saa_free(pabbrev);
2027 /* build line section */
2028 /* prolog */
2029 plines = saa_init(1L);
2030 saa_write8(plines,1); /* Minimum Instruction Length */
2031 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2032 saa_write8(plines,line_base); /* Line Base */
2033 saa_write8(plines,line_range); /* Line Range */
2034 saa_write8(plines,opcode_base); /* Opcode Base */
2035 /* standard opcode lengths (# of LEB128u operands) */
2036 saa_write8(plines,0); /* Std opcode 1 length */
2037 saa_write8(plines,1); /* Std opcode 2 length */
2038 saa_write8(plines,1); /* Std opcode 3 length */
2039 saa_write8(plines,1); /* Std opcode 4 length */
2040 saa_write8(plines,1); /* Std opcode 5 length */
2041 saa_write8(plines,0); /* Std opcode 6 length */
2042 saa_write8(plines,0); /* Std opcode 7 length */
2043 saa_write8(plines,0); /* Std opcode 8 length */
2044 saa_write8(plines,1); /* Std opcode 9 length */
2045 saa_write8(plines,0); /* Std opcode 10 length */
2046 saa_write8(plines,0); /* Std opcode 11 length */
2047 saa_write8(plines,1); /* Std opcode 12 length */
2048 /* Directory Table */
2049 saa_write8(plines,0); /* End of table */
2050 /* File Name Table */
2051 ftentry = dwarf_flist;
2052 for (indx = 0;indx<dwarf_numfiles;indx++)
2054 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2055 saa_write8(plines,0); /* directory LEB128u */
2056 saa_write8(plines,0); /* time LEB128u */
2057 saa_write8(plines,0); /* size LEB128u */
2058 ftentry = ftentry->next;
2060 saa_write8(plines,0); /* End of table */
2061 linepoff = plines->datalen;
2062 linelen = linepoff + totlen + 10;
2063 linebuf = pbuf = nasm_malloc(linelen);
2064 WRITELONG(pbuf,linelen-4); /* initial length */
2065 WRITESHORT(pbuf,3); /* dwarf version */
2066 WRITELONG(pbuf,linepoff); /* offset to line number program */
2067 /* write line header */
2068 saalen = linepoff;
2069 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2070 pbuf += linepoff;
2071 saa_free(plines);
2072 /* concatonate line program ranges */
2073 linepoff += 13;
2074 plinesrel = saa_init(1L);
2075 psect = dwarf_fsect;
2076 for (indx = 0; indx < dwarf_nsections; indx++)
2078 saa_write32(plinesrel, linepoff);
2079 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
2080 saa_write32(plinesrel, (uint32_t) 0);
2081 plinep = psect->psaa;
2082 saalen = plinep->datalen;
2083 saa_rnbytes(plinep, pbuf, saalen);
2084 pbuf += saalen;
2085 linepoff += saalen;
2086 saa_free(plinep);
2087 /* done with this entry */
2088 psect = psect->next;
2092 /* build rela.lines section */
2093 linerellen =saalen = plinesrel->datalen;
2094 linerelbuf = pbuf = nasm_malloc(linerellen);
2095 saa_rnbytes(plinesrel, pbuf, saalen);
2096 saa_free(plinesrel);
2098 /* build frame section */
2099 framelen = 4;
2100 framebuf = pbuf = nasm_malloc(framelen);
2101 WRITELONG(pbuf,framelen-4); /* initial length */
2103 /* build loc section */
2104 loclen = 16;
2105 locbuf = pbuf = nasm_malloc(loclen);
2106 WRITELONG(pbuf,0); /* null beginning offset */
2107 WRITELONG(pbuf,0); /* null ending offset */
2110 static void dwarf32_cleanup(void)
2112 if (arangesbuf)
2113 nasm_free(arangesbuf);
2114 if (arangesrelbuf)
2115 nasm_free(arangesrelbuf);
2116 if (pubnamesbuf)
2117 nasm_free(pubnamesbuf);
2118 if (infobuf)
2119 nasm_free(infobuf);
2120 if (inforelbuf)
2121 nasm_free(inforelbuf);
2122 if (abbrevbuf)
2123 nasm_free(abbrevbuf);
2124 if (linebuf)
2125 nasm_free(linebuf);
2126 if (linerelbuf)
2127 nasm_free(linerelbuf);
2128 if (framebuf)
2129 nasm_free(framebuf);
2130 if (locbuf)
2131 nasm_free(locbuf);
2133 static void dwarf32_findfile(const char * fname)
2135 int finx;
2136 struct linelist *match;
2138 /* return if fname is current file name */
2139 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2140 /* search for match */
2141 else
2143 match = 0;
2144 if (dwarf_flist)
2146 match = dwarf_flist;
2147 for (finx = 0; finx < dwarf_numfiles; finx++)
2149 if (!(strcmp(fname, match->filename)))
2151 dwarf_clist = match;
2152 return;
2156 /* add file name to end of list */
2157 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2158 dwarf_numfiles++;
2159 dwarf_clist->line = dwarf_numfiles;
2160 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2161 strcpy(dwarf_clist->filename,fname);
2162 dwarf_clist->next = 0;
2163 /* if first entry */
2164 if (!dwarf_flist)
2166 dwarf_flist = dwarf_elist = dwarf_clist;
2167 dwarf_clist->last = 0;
2169 /* chain to previous entry */
2170 else
2172 dwarf_elist->next = dwarf_clist;
2173 dwarf_elist = dwarf_clist;
2177 /* */
2178 static void dwarf32_findsect(const int index)
2180 int sinx;
2181 struct sectlist *match;
2182 struct SAA *plinep;
2183 /* return if index is current section index */
2184 if (dwarf_csect && (dwarf_csect->section == index))
2186 return;
2188 /* search for match */
2189 else
2191 match = 0;
2192 if (dwarf_fsect)
2194 match = dwarf_fsect;
2195 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2197 if ((match->section == index))
2199 dwarf_csect = match;
2200 return;
2202 match = match->next;
2205 /* add entry to end of list */
2206 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2207 dwarf_nsections++;
2208 dwarf_csect->psaa = plinep = saa_init(1L);
2209 dwarf_csect->line = 1;
2210 dwarf_csect->offset = 0;
2211 dwarf_csect->file = 1;
2212 dwarf_csect->section = index;
2213 dwarf_csect->next = 0;
2214 /* set relocatable address at start of line program */
2215 saa_write8(plinep,DW_LNS_extended_op);
2216 saa_write8(plinep,5); /* operand length */
2217 saa_write8(plinep,DW_LNE_set_address);
2218 saa_write32(plinep,0); /* Start Address */
2219 /* if first entry */
2220 if (!dwarf_fsect)
2222 dwarf_fsect = dwarf_esect = dwarf_csect;
2223 dwarf_csect->last = 0;
2225 /* chain to previous entry */
2226 else
2228 dwarf_esect->next = dwarf_csect;
2229 dwarf_esect = dwarf_csect;
2234 #endif /* OF_ELF */