compiler.h: disable __STRICT_ANSI__ for djgpp
[nasm.git] / output / outelfx32.c
blob3eeb224ffd8e49bbb9033a40b3c316d25faefc06
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2013 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 * outelfx32.c output routines for the Netwide Assembler to produce
36 * ELF32 (x86_64) 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/dwarf.h"
58 #include "output/stabs.h"
59 #include "output/outelf.h"
61 #ifdef OF_ELFX32
63 #define SECT_DELTA 32
64 static struct elf_section **sects;
65 static int nsects, sectlen;
67 #define SHSTR_DELTA 256
68 static char *shstrtab;
69 static int shstrtablen, shstrtabsize;
71 static struct SAA *syms;
72 static uint32_t nlocals, nglobs, ndebugs; /* Symbol counts */
74 static int32_t def_seg;
76 static struct RAA *bsym;
78 static struct SAA *strs;
79 static uint32_t strslen;
81 static struct elf_symbol *fwds;
83 static char elf_module[FILENAME_MAX];
85 extern struct ofmt of_elfx32;
87 static struct ELF_SECTDATA {
88 void *data;
89 int32_t len;
90 bool is_saa;
91 } *elf_sects;
92 static int elf_nsect, nsections;
93 static int32_t elf_foffs;
95 static void elf_write(void);
96 static void elf_sect_write(struct elf_section *, const void *, size_t);
97 static void elf_sect_writeaddr(struct elf_section *, int32_t, size_t);
98 static void elf_section_header(int, int, uint32_t, void *, bool, uint32_t, int, int,
99 int, int);
100 static void elf_write_sections(void);
101 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
102 static struct SAA *elf_build_reltab(uint64_t *, struct elf_reloc *);
103 static void add_sectname(char *, char *);
105 struct erel {
106 int offset, info;
109 struct symlininfo {
110 int offset;
111 int section; /* index into sects[] */
112 int segto; /* internal section number */
113 char *name; /* shallow-copied pointer of section name */
116 struct linelist {
117 struct linelist *next;
118 struct linelist *last;
119 struct symlininfo info;
120 char *filename;
121 int line;
124 struct sectlist {
125 struct SAA *psaa;
126 int section;
127 int line;
128 int offset;
129 int file;
130 struct sectlist *next;
131 struct sectlist *last;
134 /* common debug variables */
135 static int currentline = 1;
136 static int debug_immcall = 0;
138 /* stabs debug variables */
139 static struct linelist *stabslines = 0;
140 static int numlinestabs = 0;
141 static char *stabs_filename = 0;
142 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
143 static int stablen, stabstrlen, stabrellen;
145 /* dwarf debug variables */
146 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
147 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
148 static int dwarf_numfiles = 0, dwarf_nsections;
149 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
150 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
151 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
152 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
153 abbrevlen, linelen, linerellen, framelen, loclen;
154 static int32_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
157 static struct dfmt df_dwarf;
158 static struct dfmt df_stabs;
159 static struct elf_symbol *lastsym;
161 /* common debugging routines */
162 static void debugx32_typevalue(int32_t);
163 static void debugx32_deflabel(char *, int32_t, int64_t, int, char *);
164 static void debugx32_directive(const char *, const char *);
166 /* stabs debugging routines */
167 static void stabsx32_linenum(const char *filename, int32_t linenumber, int32_t);
168 static void stabsx32_output(int, void *);
169 static void stabsx32_generate(void);
170 static void stabsx32_cleanup(void);
172 /* dwarf debugging routines */
173 static void dwarfx32_init(void);
174 static void dwarfx32_linenum(const char *filename, int32_t linenumber, int32_t);
175 static void dwarfx32_output(int, void *);
176 static void dwarfx32_generate(void);
177 static void dwarfx32_cleanup(void);
178 static void dwarfx32_findfile(const char *);
179 static void dwarfx32_findsect(const int);
182 * Special section numbers which are used to define ELF special
183 * symbols, which can be used with WRT to provide PIC relocation
184 * types.
186 static int32_t elf_gotpc_sect, elf_gotoff_sect;
187 static int32_t elf_got_sect, elf_plt_sect;
188 static int32_t elf_sym_sect;
189 static int32_t elf_gottpoff_sect;
191 static void elf_init(void)
193 maxbits = 64;
194 sects = NULL;
195 nsects = sectlen = 0;
196 syms = saa_init((int32_t)sizeof(struct elf_symbol));
197 nlocals = nglobs = ndebugs = 0;
198 bsym = raa_init();
199 strs = saa_init(1L);
200 saa_wbytes(strs, "\0", 1L);
201 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
202 strslen = 2 + strlen(elf_module);
203 shstrtab = NULL;
204 shstrtablen = shstrtabsize = 0;;
205 add_sectname("", "");
207 fwds = NULL;
209 elf_gotpc_sect = seg_alloc();
210 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
211 elf_gotoff_sect = seg_alloc();
212 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
213 elf_got_sect = seg_alloc();
214 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
215 elf_plt_sect = seg_alloc();
216 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
217 elf_sym_sect = seg_alloc();
218 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
219 elf_gottpoff_sect = seg_alloc();
220 define_label("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false);
222 def_seg = seg_alloc();
226 static void elf_cleanup(int debuginfo)
228 struct elf_reloc *r;
229 int i;
231 (void)debuginfo;
233 elf_write();
234 for (i = 0; i < nsects; i++) {
235 if (sects[i]->type != SHT_NOBITS)
236 saa_free(sects[i]->data);
237 if (sects[i]->head)
238 saa_free(sects[i]->rel);
239 while (sects[i]->head) {
240 r = sects[i]->head;
241 sects[i]->head = sects[i]->head->next;
242 nasm_free(r);
245 nasm_free(sects);
246 saa_free(syms);
247 raa_free(bsym);
248 saa_free(strs);
249 if (of_elfx32.current_dfmt) {
250 of_elfx32.current_dfmt->cleanup();
254 /* add entry to the elf .shstrtab section */
255 static void add_sectname(char *firsthalf, char *secondhalf)
257 int len = strlen(firsthalf) + strlen(secondhalf);
258 while (shstrtablen + len + 1 > shstrtabsize)
259 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
260 strcpy(shstrtab + shstrtablen, firsthalf);
261 strcat(shstrtab + shstrtablen, secondhalf);
262 shstrtablen += len + 1;
265 static int elf_make_section(char *name, int type, int flags, int align)
267 struct elf_section *s;
269 s = nasm_zalloc(sizeof(*s));
271 if (type != SHT_NOBITS)
272 s->data = saa_init(1L);
273 s->tail = &s->head;
274 if (!strcmp(name, ".text"))
275 s->index = def_seg;
276 else
277 s->index = seg_alloc();
278 add_sectname("", name);
280 s->name = nasm_strdup(name);
281 s->type = type;
282 s->flags = flags;
283 s->align = align;
285 if (nsects >= sectlen)
286 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
287 sects[nsects++] = s;
289 return nsects - 1;
292 static int32_t elf_section_names(char *name, int pass, int *bits)
294 char *p;
295 uint32_t flags, flags_and, flags_or;
296 uint64_t align;
297 int type, i;
300 * Default is 64 bits.
302 if (!name) {
303 *bits = 64;
304 return def_seg;
307 p = nasm_skip_word(name);
308 if (*p)
309 *p++ = '\0';
310 flags_and = flags_or = type = align = 0;
312 elf_section_attrib(name, p, pass, &flags_and,
313 &flags_or, &align, &type);
315 if (!strcmp(name, ".shstrtab") ||
316 !strcmp(name, ".symtab") ||
317 !strcmp(name, ".strtab")) {
318 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
319 "name `%s'", name);
320 return NO_SEG;
323 for (i = 0; i < nsects; i++)
324 if (!strcmp(name, sects[i]->name))
325 break;
326 if (i == nsects) {
327 const struct elf_known_section *ks = elf_known_sections;
329 while (ks->name) {
330 if (!strcmp(name, ks->name))
331 break;
332 ks++;
335 type = type ? type : ks->type;
336 align = align ? align : ks->align;
337 flags = (ks->flags & ~flags_and) | flags_or;
339 i = elf_make_section(name, type, flags, align);
340 } else if (pass == 1) {
341 if ((type && sects[i]->type != type)
342 || (align && sects[i]->align != align)
343 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
344 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
345 " redeclaration of section `%s'", name);
348 return sects[i]->index;
351 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
352 int is_global, char *special)
354 int pos = strslen;
355 struct elf_symbol *sym;
356 bool special_used = false;
358 #if defined(DEBUG) && DEBUG>2
359 nasm_error(ERR_DEBUG,
360 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
361 name, segment, offset, is_global, special);
362 #endif
363 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
365 * This is a NASM special symbol. We never allow it into
366 * the ELF symbol table, even if it's a valid one. If it
367 * _isn't_ a valid one, we should barf immediately.
369 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
370 strcmp(name, "..got") && strcmp(name, "..plt") &&
371 strcmp(name, "..sym") && strcmp(name, "..gottpoff"))
372 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
373 return;
376 if (is_global == 3) {
377 struct elf_symbol **s;
379 * Fix up a forward-reference symbol size from the first
380 * pass.
382 for (s = &fwds; *s; s = &(*s)->nextfwd)
383 if (!strcmp((*s)->name, name)) {
384 struct tokenval tokval;
385 expr *e;
386 char *p = nasm_skip_spaces(nasm_skip_word(special));
388 stdscan_reset();
389 stdscan_set(p);
390 tokval.t_type = TOKEN_INVALID;
391 e = evaluate(stdscan, NULL, &tokval, NULL, 1, nasm_error, NULL);
392 if (e) {
393 if (!is_simple(e))
394 nasm_error(ERR_NONFATAL, "cannot use relocatable"
395 " expression as symbol size");
396 else
397 (*s)->size = reloc_value(e);
401 * Remove it from the list of unresolved sizes.
403 nasm_free((*s)->name);
404 *s = (*s)->nextfwd;
405 return;
407 return; /* it wasn't an important one */
410 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
411 strslen += 1 + strlen(name);
413 lastsym = sym = saa_wstruct(syms);
415 memset(&sym->symv, 0, sizeof(struct rbtree));
417 sym->strpos = pos;
418 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
419 sym->other = STV_DEFAULT;
420 sym->size = 0;
421 if (segment == NO_SEG)
422 sym->section = SHN_ABS;
423 else {
424 int i;
425 sym->section = SHN_UNDEF;
426 if (segment == def_seg) {
427 /* we have to be sure at least text section is there */
428 int tempint;
429 if (segment != elf_section_names(".text", 2, &tempint))
430 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
432 for (i = 0; i < nsects; i++) {
433 if (segment == sects[i]->index) {
434 sym->section = i + 1;
435 break;
440 if (is_global == 2) {
441 sym->size = offset;
442 sym->symv.key = 0;
443 sym->section = SHN_COMMON;
445 * We have a common variable. Check the special text to see
446 * if it's a valid number and power of two; if so, store it
447 * as the alignment for the common variable.
449 if (special) {
450 bool err;
451 sym->symv.key = readnum(special, &err);
452 if (err)
453 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
454 " valid number", special);
455 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
456 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
457 " power of two", special);
459 special_used = true;
460 } else
461 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
463 if (sym->type == SYM_GLOBAL) {
465 * If sym->section == SHN_ABS, then the first line of the
466 * else section would cause a core dump, because its a reference
467 * beyond the end of the section array.
468 * This behaviour is exhibited by this code:
469 * GLOBAL crash_nasm
470 * crash_nasm equ 0
471 * To avoid such a crash, such requests are silently discarded.
472 * This may not be the best solution.
474 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
475 bsym = raa_write(bsym, segment, nglobs);
476 } else if (sym->section != SHN_ABS) {
478 * This is a global symbol; so we must add it to the rbtree
479 * of global symbols in its section.
481 * In addition, we check the special text for symbol
482 * type and size information.
484 sects[sym->section-1]->gsyms =
485 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
487 if (special) {
488 int n = strcspn(special, " \t");
490 if (!nasm_strnicmp(special, "function", n))
491 sym->type |= STT_FUNC;
492 else if (!nasm_strnicmp(special, "data", n) ||
493 !nasm_strnicmp(special, "object", n))
494 sym->type |= STT_OBJECT;
495 else if (!nasm_strnicmp(special, "notype", n))
496 sym->type |= STT_NOTYPE;
497 else
498 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
499 n, special);
500 special += n;
502 special = nasm_skip_spaces(special);
503 if (*special) {
504 n = strcspn(special, " \t");
505 if (!nasm_strnicmp(special, "default", n))
506 sym->other = STV_DEFAULT;
507 else if (!nasm_strnicmp(special, "internal", n))
508 sym->other = STV_INTERNAL;
509 else if (!nasm_strnicmp(special, "hidden", n))
510 sym->other = STV_HIDDEN;
511 else if (!nasm_strnicmp(special, "protected", n))
512 sym->other = STV_PROTECTED;
513 else
514 n = 0;
515 special += n;
518 if (*special) {
519 struct tokenval tokval;
520 expr *e;
521 int fwd = 0;
522 char *saveme = stdscan_get();
524 while (special[n] && nasm_isspace(special[n]))
525 n++;
527 * We have a size expression; attempt to
528 * evaluate it.
530 stdscan_reset();
531 stdscan_set(special + n);
532 tokval.t_type = TOKEN_INVALID;
533 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, nasm_error,
534 NULL);
535 if (fwd) {
536 sym->nextfwd = fwds;
537 fwds = sym;
538 sym->name = nasm_strdup(name);
539 } else if (e) {
540 if (!is_simple(e))
541 nasm_error(ERR_NONFATAL, "cannot use relocatable"
542 " expression as symbol size");
543 else
544 sym->size = reloc_value(e);
546 stdscan_set(saveme);
548 special_used = true;
551 * If TLS segment, mark symbol accordingly.
553 if (sects[sym->section - 1]->flags & SHF_TLS) {
554 sym->type &= 0xf0;
555 sym->type |= STT_TLS;
558 sym->globnum = nglobs;
559 nglobs++;
560 } else
561 nlocals++;
563 if (special && !special_used)
564 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
567 static void elf_add_reloc(struct elf_section *sect, int32_t segment,
568 int32_t offset, int type)
570 struct elf_reloc *r;
572 r = *sect->tail = nasm_zalloc(sizeof(struct elf_reloc));
573 sect->tail = &r->next;
575 r->address = sect->len;
576 r->offset = offset;
578 if (segment != NO_SEG) {
579 int i;
580 for (i = 0; i < nsects; i++)
581 if (segment == sects[i]->index)
582 r->symbol = i + 2;
583 if (!r->symbol)
584 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
586 r->type = type;
588 sect->nrelocs++;
592 * This routine deals with ..got and ..sym relocations: the more
593 * complicated kinds. In shared-library writing, some relocations
594 * with respect to global symbols must refer to the precise symbol
595 * rather than referring to an offset from the base of the section
596 * _containing_ the symbol. Such relocations call to this routine,
597 * which searches the symbol list for the symbol in question.
599 * R_X86_64_GOT32 references require the _exact_ symbol address to be
600 * used; R_X86_64_32 references can be at an offset from the symbol.
601 * The boolean argument `exact' tells us this.
603 * Return value is the adjusted value of `addr', having become an
604 * offset from the symbol rather than the section. Should always be
605 * zero when returning from an exact call.
607 * Limitation: if you define two symbols at the same place,
608 * confusion will occur.
610 * Inefficiency: we search, currently, using a linked list which
611 * isn't even necessarily sorted.
613 static void elf_add_gsym_reloc(struct elf_section *sect,
614 int32_t segment, uint32_t offset, int32_t pcrel,
615 int type, bool exact)
617 struct elf_reloc *r;
618 struct elf_section *s;
619 struct elf_symbol *sym;
620 struct rbtree *srb;
621 int i;
624 * First look up the segment/offset pair and find a global
625 * symbol corresponding to it. If it's not one of our segments,
626 * then it must be an external symbol, in which case we're fine
627 * doing a normal elf_add_reloc after first sanity-checking
628 * that the offset from the symbol is zero.
630 s = NULL;
631 for (i = 0; i < nsects; i++)
632 if (segment == sects[i]->index) {
633 s = sects[i];
634 break;
637 if (!s) {
638 if (exact && offset)
639 nasm_error(ERR_NONFATAL, "invalid access to an external symbol");
640 else
641 elf_add_reloc(sect, segment, offset - pcrel, type);
642 return;
645 srb = rb_search(s->gsyms, offset);
646 if (!srb || (exact && srb->key != offset)) {
647 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
648 " for this reference");
649 return;
651 sym = container_of(srb, struct elf_symbol, symv);
653 r = *sect->tail = nasm_malloc(sizeof(struct elf_reloc));
654 sect->tail = &r->next;
655 r->next = NULL;
657 r->address = sect->len;
658 r->offset = offset - pcrel - sym->symv.key;
659 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
660 r->type = type;
662 sect->nrelocs++;
665 static void elf_out(int32_t segto, const void *data,
666 enum out_type type, uint64_t size,
667 int32_t segment, int32_t wrt)
669 struct elf_section *s;
670 int32_t addr;
671 int reltype, bytes;
672 int i;
673 static struct symlininfo sinfo;
675 #if defined(DEBUG) && DEBUG>2
676 if (data)
677 nasm_error(ERR_DEBUG,
678 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64" data: %"PRIx64"\n",
679 currentline, type, segment, segto, size, *(int64_t *)data);
680 else
681 nasm_error(ERR_DEBUG,
682 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64"\n",
683 currentline, type, segment, segto, size);
684 #endif
687 * handle absolute-assembly (structure definitions)
689 if (segto == NO_SEG) {
690 if (type != OUT_RESERVE)
691 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
692 " space");
693 return;
696 s = NULL;
697 for (i = 0; i < nsects; i++)
698 if (segto == sects[i]->index) {
699 s = sects[i];
700 break;
702 if (!s) {
703 int tempint; /* ignored */
704 if (segto != elf_section_names(".text", 2, &tempint))
705 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
706 else {
707 s = sects[nsects - 1];
708 i = nsects - 1;
712 /* again some stabs debugging stuff */
713 if (of_elfx32.current_dfmt) {
714 sinfo.offset = s->len;
715 sinfo.section = i;
716 sinfo.segto = segto;
717 sinfo.name = s->name;
718 of_elfx32.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
720 /* end of debugging stuff */
722 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
723 nasm_error(ERR_WARNING, "attempt to initialize memory in"
724 " BSS section `%s': ignored", s->name);
725 s->len += realsize(type, size);
726 return;
729 switch (type) {
730 case OUT_RESERVE:
731 if (s->type == SHT_PROGBITS) {
732 nasm_error(ERR_WARNING, "uninitialized space declared in"
733 " non-BSS section `%s': zeroing", s->name);
734 elf_sect_write(s, NULL, size);
735 } else
736 s->len += size;
737 break;
739 case OUT_RAWDATA:
740 if (segment != NO_SEG)
741 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
742 elf_sect_write(s, data, size);
743 break;
745 case OUT_ADDRESS:
747 int isize = (int)size;
748 int asize = abs(size);
750 addr = *(int64_t *)data;
751 if (segment == NO_SEG) {
752 /* Do nothing */
753 } else if (segment % 2) {
754 nasm_error(ERR_NONFATAL, "ELF format does not support"
755 " segment base references");
756 } else {
757 if (wrt == NO_SEG) {
758 switch (isize) {
759 case 1:
760 case -1:
761 elf_add_reloc(s, segment, addr, R_X86_64_8);
762 break;
763 case 2:
764 case -2:
765 elf_add_reloc(s, segment, addr, R_X86_64_16);
766 break;
767 case 4:
768 elf_add_reloc(s, segment, addr, R_X86_64_32);
769 break;
770 case -4:
771 elf_add_reloc(s, segment, addr, R_X86_64_32S);
772 break;
773 case 8:
774 case -8:
775 elf_add_reloc(s, segment, addr, R_X86_64_64);
776 break;
777 default:
778 nasm_error(ERR_PANIC, "internal error elfx32-hpa-871");
779 break;
781 addr = 0;
782 } else if (wrt == elf_gotpc_sect + 1) {
784 * The user will supply GOT relative to $$. ELF
785 * will let us have GOT relative to $. So we
786 * need to fix up the data item by $-$$.
788 addr += s->len;
789 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
790 addr = 0;
791 } else if (wrt == elf_gotoff_sect + 1) {
792 nasm_error(ERR_NONFATAL, "ELFX32 doesn't support "
793 "R_X86_64_GOTOFF64");
794 } else if (wrt == elf_got_sect + 1) {
795 switch (asize) {
796 case 4:
797 elf_add_gsym_reloc(s, segment, addr, 0,
798 R_X86_64_GOT32, true);
799 addr = 0;
800 break;
801 default:
802 nasm_error(ERR_NONFATAL, "invalid ..got reference");
803 break;
805 } else if (wrt == elf_sym_sect + 1) {
806 switch (isize) {
807 case 1:
808 case -1:
809 elf_add_gsym_reloc(s, segment, addr, 0,
810 R_X86_64_8, false);
811 addr = 0;
812 break;
813 case 2:
814 case -2:
815 elf_add_gsym_reloc(s, segment, addr, 0,
816 R_X86_64_16, false);
817 addr = 0;
818 break;
819 case 4:
820 elf_add_gsym_reloc(s, segment, addr, 0,
821 R_X86_64_32, false);
822 addr = 0;
823 break;
824 case -4:
825 elf_add_gsym_reloc(s, segment, addr, 0,
826 R_X86_64_32S, false);
827 addr = 0;
828 break;
829 case 8:
830 case -8:
831 elf_add_gsym_reloc(s, segment, addr, 0,
832 R_X86_64_64, false);
833 addr = 0;
834 break;
835 default:
836 nasm_error(ERR_PANIC, "internal error elfx32-hpa-903");
837 break;
839 } else if (wrt == elf_plt_sect + 1) {
840 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
841 "relative PLT references");
842 } else {
843 nasm_error(ERR_NONFATAL, "ELF format does not support this"
844 " use of WRT");
847 elf_sect_writeaddr(s, addr, asize);
848 break;
851 case OUT_REL1ADR:
852 reltype = R_X86_64_PC8;
853 bytes = 1;
854 goto rel12adr;
856 case OUT_REL2ADR:
857 reltype = R_X86_64_PC16;
858 bytes = 2;
859 goto rel12adr;
861 rel12adr:
862 addr = *(int64_t *)data - size;
863 if (segment == segto)
864 nasm_error(ERR_PANIC, "intra-segment OUT_REL1ADR");
865 if (segment == NO_SEG) {
866 /* Do nothing */
867 } else if (segment % 2) {
868 nasm_error(ERR_NONFATAL, "ELF format does not support"
869 " segment base references");
870 } else {
871 if (wrt == NO_SEG) {
872 elf_add_reloc(s, segment, addr, reltype);
873 addr = 0;
874 } else {
875 nasm_error(ERR_NONFATAL,
876 "Unsupported non-32-bit ELF relocation");
879 elf_sect_writeaddr(s, addr, bytes);
880 break;
882 case OUT_REL4ADR:
883 addr = *(int64_t *)data - size;
884 if (segment == segto)
885 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
886 if (segment == NO_SEG) {
887 /* Do nothing */
888 } else if (segment % 2) {
889 nasm_error(ERR_NONFATAL, "ELFX32 format does not support"
890 " segment base references");
891 } else {
892 if (wrt == NO_SEG) {
893 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
894 addr = 0;
895 } else if (wrt == elf_plt_sect + 1) {
896 elf_add_gsym_reloc(s, segment, addr+size, size,
897 R_X86_64_PLT32, true);
898 addr = 0;
899 } else if (wrt == elf_gotpc_sect + 1 ||
900 wrt == elf_got_sect + 1) {
901 elf_add_gsym_reloc(s, segment, addr+size, size,
902 R_X86_64_GOTPCREL, true);
903 addr = 0;
904 } else if (wrt == elf_gotoff_sect + 1 ||
905 wrt == elf_got_sect + 1) {
906 nasm_error(ERR_NONFATAL, "invalid ..gotoff reference");
907 } else if (wrt == elf_gottpoff_sect + 1) {
908 elf_add_gsym_reloc(s, segment, addr+size, size,
909 R_X86_64_GOTTPOFF, true);
910 addr = 0;
911 } else {
912 nasm_error(ERR_NONFATAL, "ELFX32 format does not support this"
913 " use of WRT");
916 elf_sect_writeaddr(s, addr, 4);
917 break;
919 case OUT_REL8ADR:
920 nasm_error(ERR_NONFATAL,
921 "32-bit ELF format does not support 64-bit relocations");
922 addr = 0;
923 elf_sect_writeaddr(s, addr, 8);
924 break;
928 static void elf_write(void)
930 int align;
931 char *p;
932 int i;
934 struct SAA *symtab;
935 int32_t symtablen, symtablocal;
938 * Work out how many sections we will have. We have SHN_UNDEF,
939 * then the flexible user sections, then the fixed sections
940 * `.shstrtab', `.symtab' and `.strtab', then optionally
941 * relocation sections for the user sections.
943 nsections = sec_numspecial + 1;
944 if (of_elfx32.current_dfmt == &df_stabs)
945 nsections += 3;
946 else if (of_elfx32.current_dfmt == &df_dwarf)
947 nsections += 10;
949 add_sectname("", ".shstrtab");
950 add_sectname("", ".symtab");
951 add_sectname("", ".strtab");
952 for (i = 0; i < nsects; i++) {
953 nsections++; /* for the section itself */
954 if (sects[i]->head) {
955 nsections++; /* for its relocations */
956 add_sectname(".rela", sects[i]->name);
960 if (of_elfx32.current_dfmt == &df_stabs) {
961 /* in case the debug information is wanted, just add these three sections... */
962 add_sectname("", ".stab");
963 add_sectname("", ".stabstr");
964 add_sectname(".rel", ".stab");
967 else if (of_elfx32.current_dfmt == &df_dwarf) {
968 /* the dwarf debug standard specifies the following ten sections,
969 not all of which are currently implemented,
970 although all of them are defined. */
971 add_sectname("", ".debug_aranges");
972 add_sectname(".rela", ".debug_aranges");
973 add_sectname("", ".debug_pubnames");
974 add_sectname("", ".debug_info");
975 add_sectname(".rela", ".debug_info");
976 add_sectname("", ".debug_abbrev");
977 add_sectname("", ".debug_line");
978 add_sectname(".rela", ".debug_line");
979 add_sectname("", ".debug_frame");
980 add_sectname("", ".debug_loc");
984 * Output the ELF header.
986 nasm_write("\177ELF\1\1\1", 7, ofile);
987 fputc(elf_osabi, ofile);
988 fputc(elf_abiver, ofile);
989 fwritezero(7, ofile);
990 fwriteint16_t(ET_REL, ofile); /* relocatable file */
991 fwriteint16_t(EM_X86_64, ofile); /* processor ID */
992 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
993 fwriteint32_t(0L, ofile); /* no entry point */
994 fwriteint32_t(0L, ofile); /* no program header table */
995 fwriteint32_t(0x40L, ofile); /* section headers straight after
996 * ELF header plus alignment */
997 fwriteint32_t(0L, ofile); /* X86_64 defines no special flags */
998 fwriteint16_t(0x34, ofile); /* size of ELF header */
999 fwriteint16_t(0, ofile); /* no program header table, again */
1000 fwriteint16_t(0, ofile); /* still no program header table */
1001 fwriteint16_t(sizeof(Elf32_Shdr), ofile); /* size of section header */
1002 fwriteint16_t(nsections, ofile); /* number of sections */
1003 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
1004 * section header table */
1005 fwriteint32_t(0L, ofile); /* align to 0x40 bytes */
1006 fwriteint32_t(0L, ofile);
1007 fwriteint32_t(0L, ofile);
1010 * Build the symbol table and relocation tables.
1012 symtab = elf_build_symtab(&symtablen, &symtablocal);
1013 for (i = 0; i < nsects; i++)
1014 if (sects[i]->head)
1015 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1016 sects[i]->head);
1019 * Now output the section header table.
1022 elf_foffs = 0x40 + sizeof(Elf32_Shdr) * nsections;
1023 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1024 elf_foffs += align;
1025 elf_nsect = 0;
1026 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1028 /* SHN_UNDEF */
1029 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1030 p = shstrtab + 1;
1032 /* The normal sections */
1033 for (i = 0; i < nsects; i++) {
1034 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1035 (sects[i]->type == SHT_PROGBITS ?
1036 sects[i]->data : NULL), true,
1037 sects[i]->len, 0, 0, sects[i]->align, 0);
1038 p += strlen(p) + 1;
1041 /* .shstrtab */
1042 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1043 shstrtablen, 0, 0, 1, 0);
1044 p += strlen(p) + 1;
1046 /* .symtab */
1047 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1048 symtablen, sec_strtab, symtablocal, 4, 16);
1049 p += strlen(p) + 1;
1051 /* .strtab */
1052 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1053 strslen, 0, 0, 1, 0);
1054 p += strlen(p) + 1;
1056 /* The relocation sections */
1057 for (i = 0; i < nsects; i++)
1058 if (sects[i]->head) {
1059 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1060 sects[i]->rellen, sec_symtab, i + 1, 4, 12);
1061 p += strlen(p) + 1;
1064 if (of_elfx32.current_dfmt == &df_stabs) {
1065 /* for debugging information, create the last three sections
1066 which are the .stab , .stabstr and .rel.stab sections respectively */
1068 /* this function call creates the stab sections in memory */
1069 stabsx32_generate();
1071 if (stabbuf && stabstrbuf && stabrelbuf) {
1072 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1073 stablen, sec_stabstr, 0, 4, 12);
1074 p += strlen(p) + 1;
1076 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1077 stabstrlen, 0, 0, 4, 0);
1078 p += strlen(p) + 1;
1080 /* link -> symtable info -> section to refer to */
1081 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1082 stabrellen, sec_symtab, sec_stab, 4, 8);
1083 p += strlen(p) + 1;
1085 } else if (of_elfx32.current_dfmt == &df_dwarf) {
1086 /* for dwarf debugging information, create the ten dwarf sections */
1088 /* this function call creates the dwarf sections in memory */
1089 if (dwarf_fsect)
1090 dwarfx32_generate();
1092 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1093 arangeslen, 0, 0, 1, 0);
1094 p += strlen(p) + 1;
1096 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1097 arangesrellen, sec_symtab, sec_debug_aranges, 1, 12);
1098 p += strlen(p) + 1;
1100 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1101 pubnameslen, 0, 0, 1, 0);
1102 p += strlen(p) + 1;
1104 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1105 infolen, 0, 0, 1, 0);
1106 p += strlen(p) + 1;
1108 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1109 inforellen, sec_symtab, sec_debug_info, 1, 12);
1110 p += strlen(p) + 1;
1112 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1113 abbrevlen, 0, 0, 1, 0);
1114 p += strlen(p) + 1;
1116 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1117 linelen, 0, 0, 1, 0);
1118 p += strlen(p) + 1;
1120 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1121 linerellen, sec_symtab, sec_debug_line, 1, 12);
1122 p += strlen(p) + 1;
1124 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1125 framelen, 0, 0, 8, 0);
1126 p += strlen(p) + 1;
1128 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1129 loclen, 0, 0, 1, 0);
1130 p += strlen(p) + 1;
1132 fwritezero(align, ofile);
1135 * Now output the sections.
1137 elf_write_sections();
1139 nasm_free(elf_sects);
1140 saa_free(symtab);
1143 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1145 struct SAA *s = saa_init(1L);
1146 struct elf_symbol *sym;
1147 uint8_t entry[24], *p;
1148 int i;
1150 *len = *local = 0;
1153 * First, an all-zeros entry, required by the ELF spec.
1155 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1156 *len += 16;
1157 (*local)++;
1160 * Next, an entry for the file name.
1162 p = entry;
1163 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1164 WRITELONG(p, 0); /* no value */
1165 WRITELONG(p, 0); /* no size either */
1166 WRITESHORT(p, STT_FILE); /* type FILE */
1167 WRITESHORT(p, SHN_ABS);
1168 saa_wbytes(s, entry, 16L);
1169 *len += 16;
1170 (*local)++;
1173 * Now some standard symbols defining the segments, for relocation
1174 * purposes.
1176 for (i = 1; i <= nsects; i++) {
1177 p = entry;
1178 WRITELONG(p, 0); /* no symbol name */
1179 WRITELONG(p, 0); /* offset zero */
1180 WRITELONG(p, 0); /* size zero */
1181 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1182 WRITESHORT(p, i); /* section id */
1183 saa_wbytes(s, entry, 16L);
1184 *len += 16;
1185 (*local)++;
1190 * Now the other local symbols.
1192 saa_rewind(syms);
1193 while ((sym = saa_rstruct(syms))) {
1194 if (sym->type & SYM_GLOBAL)
1195 continue;
1196 p = entry;
1197 WRITELONG(p, sym->strpos); /* index into symbol string table */
1198 WRITELONG(p, sym->symv.key); /* value of symbol */
1199 WRITELONG(p, sym->size); /* size of symbol */
1200 WRITECHAR(p, sym->type); /* type and binding */
1201 WRITECHAR(p, sym->other); /* visibility */
1202 WRITESHORT(p, sym->section); /* index into section header table */
1203 saa_wbytes(s, entry, 16L);
1204 *len += 16;
1205 (*local)++;
1208 * dwarf needs symbols for debug sections
1209 * which are relocation targets.
1211 if (of_elfx32.current_dfmt == &df_dwarf) {
1212 dwarf_infosym = *local;
1213 p = entry;
1214 WRITELONG(p, 0); /* no symbol name */
1215 WRITELONG(p, 0); /* offset zero */
1216 WRITELONG(p, 0); /* size zero */
1217 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1218 WRITESHORT(p, sec_debug_info); /* section id */
1219 saa_wbytes(s, entry, 16L);
1220 *len += 16;
1221 (*local)++;
1222 dwarf_abbrevsym = *local;
1223 p = entry;
1224 WRITELONG(p, 0); /* no symbol name */
1225 WRITELONG(p, 0); /* offset zero */
1226 WRITELONG(p, 0); /* size zero */
1227 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1228 WRITESHORT(p, sec_debug_abbrev); /* section id */
1229 saa_wbytes(s, entry, 16L);
1230 *len += 16;
1231 (*local)++;
1232 dwarf_linesym = *local;
1233 p = entry;
1234 WRITELONG(p, 0); /* no symbol name */
1235 WRITELONG(p, 0); /* offset zero */
1236 WRITELONG(p, 0); /* size zero */
1237 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1238 WRITESHORT(p, sec_debug_line); /* section id */
1239 saa_wbytes(s, entry, 16L);
1240 *len += 16;
1241 (*local)++;
1245 * Now the global symbols.
1247 saa_rewind(syms);
1248 while ((sym = saa_rstruct(syms))) {
1249 if (!(sym->type & SYM_GLOBAL))
1250 continue;
1251 p = entry;
1252 WRITELONG(p, sym->strpos);
1253 WRITELONG(p, sym->symv.key);
1254 WRITELONG(p, sym->size);
1255 WRITECHAR(p, sym->type); /* type and binding */
1256 WRITECHAR(p, sym->other); /* visibility */
1257 WRITESHORT(p, sym->section);
1258 saa_wbytes(s, entry, 16L);
1259 *len += 16;
1262 return s;
1265 static struct SAA *elf_build_reltab(uint64_t *len, struct elf_reloc *r)
1267 struct SAA *s;
1268 uint8_t *p, entry[12];
1269 int32_t global_offset;
1271 if (!r)
1272 return NULL;
1274 s = saa_init(1L);
1275 *len = 0;
1278 * How to onvert from a global placeholder to a real symbol index;
1279 * the +2 refers to the two special entries, the null entry and
1280 * the filename entry.
1282 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1284 while (r) {
1285 int32_t sym = r->symbol;
1287 if (sym >= GLOBAL_TEMP_BASE)
1288 sym += global_offset;
1290 p = entry;
1291 WRITELONG(p, r->address);
1292 WRITELONG(p, (sym << 8) + r->type);
1293 WRITELONG(p, r->offset);
1294 saa_wbytes(s, entry, 12L);
1295 *len += 12;
1297 r = r->next;
1300 return s;
1303 static void elf_section_header(int name, int type, uint32_t flags,
1304 void *data, bool is_saa, uint32_t datalen,
1305 int link, int info, int align, int eltsize)
1307 elf_sects[elf_nsect].data = data;
1308 elf_sects[elf_nsect].len = datalen;
1309 elf_sects[elf_nsect].is_saa = is_saa;
1310 elf_nsect++;
1312 fwriteint32_t((int32_t)name, ofile);
1313 fwriteint32_t((int32_t)type, ofile);
1314 fwriteint32_t((int32_t)flags, ofile);
1315 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
1316 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
1317 fwriteint32_t(datalen, ofile);
1318 if (data)
1319 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
1320 fwriteint32_t((int32_t)link, ofile);
1321 fwriteint32_t((int32_t)info, ofile);
1322 fwriteint32_t((int32_t)align, ofile);
1323 fwriteint32_t((int32_t)eltsize, ofile);
1326 static void elf_write_sections(void)
1328 int i;
1329 for (i = 0; i < elf_nsect; i++)
1330 if (elf_sects[i].data) {
1331 int32_t len = elf_sects[i].len;
1332 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
1333 int32_t align = reallen - len;
1334 if (elf_sects[i].is_saa)
1335 saa_fpwrite(elf_sects[i].data, ofile);
1336 else
1337 nasm_write(elf_sects[i].data, len, ofile);
1338 fwritezero(align, ofile);
1342 static void elf_sect_write(struct elf_section *sect, const void *data, size_t len)
1344 saa_wbytes(sect->data, data, len);
1345 sect->len += len;
1347 static void elf_sect_writeaddr(struct elf_section *sect, int32_t data, size_t len)
1349 saa_writeaddr(sect->data, data, len);
1350 sect->len += len;
1353 static void elf_sectalign(int32_t seg, unsigned int value)
1355 struct elf_section *s = NULL;
1356 int i;
1358 for (i = 0; i < nsects; i++) {
1359 if (sects[i]->index == seg) {
1360 s = sects[i];
1361 break;
1364 if (!s || !is_power2(value))
1365 return;
1367 if (value > s->align)
1368 s->align = value;
1371 static int32_t elf_segbase(int32_t segment)
1373 return segment;
1376 static void elf_filename(char *inname, char *outname)
1378 strcpy(elf_module, inname);
1379 standard_extension(inname, outname, ".o");
1382 extern macros_t elf_stdmac[];
1384 static int elf_set_info(enum geninfo type, char **val)
1386 (void)type;
1387 (void)val;
1388 return 0;
1390 static struct dfmt df_dwarf = {
1391 "ELFX32 (x86-64) dwarf debug format for Linux/Unix",
1392 "dwarf",
1393 dwarfx32_init,
1394 dwarfx32_linenum,
1395 debugx32_deflabel,
1396 debugx32_directive,
1397 debugx32_typevalue,
1398 dwarfx32_output,
1399 dwarfx32_cleanup
1401 static struct dfmt df_stabs = {
1402 "ELFX32 (x86-64) stabs debug format for Linux/Unix",
1403 "stabs",
1404 null_debug_init,
1405 stabsx32_linenum,
1406 debugx32_deflabel,
1407 debugx32_directive,
1408 debugx32_typevalue,
1409 stabsx32_output,
1410 stabsx32_cleanup
1413 struct dfmt *elfx32_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1415 struct ofmt of_elfx32 = {
1416 "ELFX32 (x86_64) object files (e.g. Linux)",
1417 "elfx32",
1419 elfx32_debugs_arr,
1420 &df_stabs,
1421 elf_stdmac,
1422 elf_init,
1423 elf_set_info,
1424 elf_out,
1425 elf_deflabel,
1426 elf_section_names,
1427 elf_sectalign,
1428 elf_segbase,
1429 elf_directive,
1430 elf_filename,
1431 elf_cleanup
1434 /* common debugging routines */
1435 static void debugx32_deflabel(char *name, int32_t segment, int64_t offset,
1436 int is_global, char *special)
1438 (void)name;
1439 (void)segment;
1440 (void)offset;
1441 (void)is_global;
1442 (void)special;
1445 static void debugx32_directive(const char *directive, const char *params)
1447 (void)directive;
1448 (void)params;
1451 static void debugx32_typevalue(int32_t type)
1453 int32_t stype, ssize;
1454 switch (TYM_TYPE(type)) {
1455 case TY_LABEL:
1456 ssize = 0;
1457 stype = STT_NOTYPE;
1458 break;
1459 case TY_BYTE:
1460 ssize = 1;
1461 stype = STT_OBJECT;
1462 break;
1463 case TY_WORD:
1464 ssize = 2;
1465 stype = STT_OBJECT;
1466 break;
1467 case TY_DWORD:
1468 ssize = 4;
1469 stype = STT_OBJECT;
1470 break;
1471 case TY_FLOAT:
1472 ssize = 4;
1473 stype = STT_OBJECT;
1474 break;
1475 case TY_QWORD:
1476 ssize = 8;
1477 stype = STT_OBJECT;
1478 break;
1479 case TY_TBYTE:
1480 ssize = 10;
1481 stype = STT_OBJECT;
1482 break;
1483 case TY_OWORD:
1484 ssize = 16;
1485 stype = STT_OBJECT;
1486 break;
1487 case TY_YWORD:
1488 ssize = 32;
1489 stype = STT_OBJECT;
1490 break;
1491 case TY_COMMON:
1492 ssize = 0;
1493 stype = STT_COMMON;
1494 break;
1495 case TY_SEG:
1496 ssize = 0;
1497 stype = STT_SECTION;
1498 break;
1499 case TY_EXTERN:
1500 ssize = 0;
1501 stype = STT_NOTYPE;
1502 break;
1503 case TY_EQU:
1504 ssize = 0;
1505 stype = STT_NOTYPE;
1506 break;
1507 default:
1508 ssize = 0;
1509 stype = STT_NOTYPE;
1510 break;
1512 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1513 lastsym->size = ssize;
1514 lastsym->type = stype;
1518 /* stabs debugging routines */
1520 static void stabsx32_linenum(const char *filename, int32_t linenumber, int32_t segto)
1522 (void)segto;
1523 if (!stabs_filename) {
1524 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1525 strcpy(stabs_filename, filename);
1526 } else {
1527 if (strcmp(stabs_filename, filename)) {
1528 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1529 in fact, this leak comes in quite handy to maintain a list of files
1530 encountered so far in the symbol lines... */
1532 /* why not nasm_free(stabs_filename); we're done with the old one */
1534 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1535 strcpy(stabs_filename, filename);
1538 debug_immcall = 1;
1539 currentline = linenumber;
1543 static void stabsx32_output(int type, void *param)
1545 struct symlininfo *s;
1546 struct linelist *el;
1547 if (type == TY_DEBUGSYMLIN) {
1548 if (debug_immcall) {
1549 s = (struct symlininfo *)param;
1550 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1551 return; /* line info is only collected for executable sections */
1552 numlinestabs++;
1553 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1554 el->info.offset = s->offset;
1555 el->info.section = s->section;
1556 el->info.name = s->name;
1557 el->line = currentline;
1558 el->filename = stabs_filename;
1559 el->next = 0;
1560 if (stabslines) {
1561 stabslines->last->next = el;
1562 stabslines->last = el;
1563 } else {
1564 stabslines = el;
1565 stabslines->last = el;
1569 debug_immcall = 0;
1572 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1574 static void stabsx32_generate(void)
1576 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1577 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1578 char **allfiles;
1579 int *fileidx;
1581 struct linelist *ptr;
1583 ptr = stabslines;
1585 allfiles = (char **)nasm_zalloc(numlinestabs * sizeof(char *));
1586 numfiles = 0;
1587 while (ptr) {
1588 if (numfiles == 0) {
1589 allfiles[0] = ptr->filename;
1590 numfiles++;
1591 } else {
1592 for (i = 0; i < numfiles; i++) {
1593 if (!strcmp(allfiles[i], ptr->filename))
1594 break;
1596 if (i >= numfiles) {
1597 allfiles[i] = ptr->filename;
1598 numfiles++;
1601 ptr = ptr->next;
1603 strsize = 1;
1604 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1605 for (i = 0; i < numfiles; i++) {
1606 fileidx[i] = strsize;
1607 strsize += strlen(allfiles[i]) + 1;
1609 mainfileindex = 0;
1610 for (i = 0; i < numfiles; i++) {
1611 if (!strcmp(allfiles[i], elf_module)) {
1612 mainfileindex = i;
1613 break;
1618 * worst case size of the stab buffer would be:
1619 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1620 * plus one "ending" entry
1622 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1623 sizeof(struct stabentry));
1624 ssbuf = (uint8_t *)nasm_malloc(strsize);
1625 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1626 rptr = rbuf;
1628 for (i = 0; i < numfiles; i++)
1629 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1630 ssbuf[0] = 0;
1632 stabstrlen = strsize; /* set global variable for length of stab strings */
1634 sptr = sbuf;
1635 ptr = stabslines;
1636 numstabs = 0;
1638 if (ptr) {
1640 * this is the first stab, its strx points to the filename of the
1641 * the source-file, the n_desc field should be set to the number
1642 * of remaining stabs
1644 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, stabstrlen);
1646 /* this is the stab for the main source file */
1647 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1649 /* relocation table entry */
1652 * Since the symbol table has two entries before
1653 * the section symbols, the index in the info.section
1654 * member must be adjusted by adding 2
1657 WRITELONG(rptr, (sptr - sbuf) - 4);
1658 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
1660 numstabs++;
1661 currfile = mainfileindex;
1664 while (ptr) {
1665 if (strcmp(allfiles[currfile], ptr->filename)) {
1666 /* oops file has changed... */
1667 for (i = 0; i < numfiles; i++)
1668 if (!strcmp(allfiles[i], ptr->filename))
1669 break;
1670 currfile = i;
1671 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1672 ptr->info.offset);
1673 numstabs++;
1675 /* relocation table entry */
1677 WRITELONG(rptr, (sptr - sbuf) - 4);
1678 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
1681 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1682 numstabs++;
1684 /* relocation table entry */
1686 WRITELONG(rptr, (sptr - sbuf) - 4);
1687 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
1689 ptr = ptr->next;
1693 /* this is an "ending" token */
1694 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1695 numstabs++;
1697 ((struct stabentry *)sbuf)->n_desc = numstabs;
1699 nasm_free(allfiles);
1700 nasm_free(fileidx);
1702 stablen = (sptr - sbuf);
1703 stabrellen = (rptr - rbuf);
1704 stabrelbuf = rbuf;
1705 stabbuf = sbuf;
1706 stabstrbuf = ssbuf;
1709 static void stabsx32_cleanup(void)
1711 struct linelist *ptr, *del;
1712 if (!stabslines)
1713 return;
1715 ptr = stabslines;
1716 while (ptr) {
1717 del = ptr;
1718 ptr = ptr->next;
1719 nasm_free(del);
1722 nasm_free(stabbuf);
1723 nasm_free(stabrelbuf);
1724 nasm_free(stabstrbuf);
1727 /* dwarf routines */
1729 static void dwarfx32_init(void)
1731 ndebugs = 3; /* 3 debug symbols */
1734 static void dwarfx32_linenum(const char *filename, int32_t linenumber,
1735 int32_t segto)
1737 (void)segto;
1738 dwarfx32_findfile(filename);
1739 debug_immcall = 1;
1740 currentline = linenumber;
1743 /* called from elf_out with type == TY_DEBUGSYMLIN */
1744 static void dwarfx32_output(int type, void *param)
1746 int ln, aa, inx, maxln, soc;
1747 struct symlininfo *s;
1748 struct SAA *plinep;
1750 (void)type;
1752 s = (struct symlininfo *)param;
1754 /* line number info is only gathered for executable sections */
1755 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1756 return;
1758 /* Check if section index has changed */
1759 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1760 dwarfx32_findsect(s->section);
1762 /* do nothing unless line or file has changed */
1763 if (!debug_immcall)
1764 return;
1766 ln = currentline - dwarf_csect->line;
1767 aa = s->offset - dwarf_csect->offset;
1768 inx = dwarf_clist->line;
1769 plinep = dwarf_csect->psaa;
1770 /* check for file change */
1771 if (!(inx == dwarf_csect->file)) {
1772 saa_write8(plinep,DW_LNS_set_file);
1773 saa_write8(plinep,inx);
1774 dwarf_csect->file = inx;
1776 /* check for line change */
1777 if (ln) {
1778 /* test if in range of special op code */
1779 maxln = line_base + line_range;
1780 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1781 if (ln >= line_base && ln < maxln && soc < 256) {
1782 saa_write8(plinep,soc);
1783 } else {
1784 saa_write8(plinep,DW_LNS_advance_line);
1785 saa_wleb128s(plinep,ln);
1786 if (aa) {
1787 saa_write8(plinep,DW_LNS_advance_pc);
1788 saa_wleb128u(plinep,aa);
1791 dwarf_csect->line = currentline;
1792 dwarf_csect->offset = s->offset;
1795 /* show change handled */
1796 debug_immcall = 0;
1800 static void dwarfx32_generate(void)
1802 uint8_t *pbuf;
1803 int indx;
1804 struct linelist *ftentry;
1805 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1806 struct SAA *parangesrel, *plinesrel, *pinforel;
1807 struct sectlist *psect;
1808 size_t saalen, linepoff, totlen, highaddr;
1810 /* write epilogues for each line program range */
1811 /* and build aranges section */
1812 paranges = saa_init(1L);
1813 parangesrel = saa_init(1L);
1814 saa_write16(paranges,3); /* dwarf version */
1815 saa_write32(parangesrel, paranges->datalen+4);
1816 saa_write32(parangesrel, (dwarf_infosym << 8) + R_X86_64_32); /* reloc to info */
1817 saa_write32(parangesrel, 0);
1818 saa_write32(paranges,0); /* offset into info */
1819 saa_write8(paranges,4); /* pointer size */
1820 saa_write8(paranges,0); /* not segmented */
1821 saa_write32(paranges,0); /* padding */
1822 /* iterate though sectlist entries */
1823 psect = dwarf_fsect;
1824 totlen = 0;
1825 highaddr = 0;
1826 for (indx = 0; indx < dwarf_nsections; indx++)
1828 plinep = psect->psaa;
1829 /* Line Number Program Epilogue */
1830 saa_write8(plinep,2); /* std op 2 */
1831 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1832 saa_write8(plinep,DW_LNS_extended_op);
1833 saa_write8(plinep,1); /* operand length */
1834 saa_write8(plinep,DW_LNE_end_sequence);
1835 totlen += plinep->datalen;
1836 /* range table relocation entry */
1837 saa_write32(parangesrel, paranges->datalen + 4);
1838 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_X86_64_32);
1839 saa_write32(parangesrel, (uint32_t) 0);
1840 /* range table entry */
1841 saa_write32(paranges,0x0000); /* range start */
1842 saa_write32(paranges,sects[psect->section]->len); /* range length */
1843 highaddr += sects[psect->section]->len;
1844 /* done with this entry */
1845 psect = psect->next;
1847 saa_write32(paranges,0); /* null address */
1848 saa_write32(paranges,0); /* null length */
1849 saalen = paranges->datalen;
1850 arangeslen = saalen + 4;
1851 arangesbuf = pbuf = nasm_malloc(arangeslen);
1852 WRITELONG(pbuf,saalen); /* initial length */
1853 saa_rnbytes(paranges, pbuf, saalen);
1854 saa_free(paranges);
1856 /* build rela.aranges section */
1857 arangesrellen = saalen = parangesrel->datalen;
1858 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1859 saa_rnbytes(parangesrel, pbuf, saalen);
1860 saa_free(parangesrel);
1862 /* build pubnames section */
1863 ppubnames = saa_init(1L);
1864 saa_write16(ppubnames,3); /* dwarf version */
1865 saa_write32(ppubnames,0); /* offset into info */
1866 saa_write32(ppubnames,0); /* space used in info */
1867 saa_write32(ppubnames,0); /* end of list */
1868 saalen = ppubnames->datalen;
1869 pubnameslen = saalen + 4;
1870 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1871 WRITELONG(pbuf,saalen); /* initial length */
1872 saa_rnbytes(ppubnames, pbuf, saalen);
1873 saa_free(ppubnames);
1875 /* build info section */
1876 pinfo = saa_init(1L);
1877 pinforel = saa_init(1L);
1878 saa_write16(pinfo,3); /* dwarf version */
1879 saa_write32(pinforel, pinfo->datalen + 4);
1880 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_X86_64_32); /* reloc to abbrev */
1881 saa_write32(pinforel, 0);
1882 saa_write32(pinfo,0); /* offset into abbrev */
1883 saa_write8(pinfo,4); /* pointer size */
1884 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1885 saa_write32(pinforel, pinfo->datalen + 4);
1886 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
1887 saa_write32(pinforel, 0);
1888 saa_write32(pinfo,0); /* DW_AT_low_pc */
1889 saa_write32(pinforel, pinfo->datalen + 4);
1890 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
1891 saa_write32(pinforel, 0);
1892 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
1893 saa_write32(pinforel, pinfo->datalen + 4);
1894 saa_write32(pinforel, (dwarf_linesym << 8) + R_X86_64_32); /* reloc to line */
1895 saa_write32(pinforel, 0);
1896 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1897 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1898 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1899 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1900 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1901 saa_write32(pinforel, pinfo->datalen + 4);
1902 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
1903 saa_write32(pinforel, 0);
1904 saa_write32(pinfo,0); /* DW_AT_low_pc */
1905 saa_write32(pinfo,0); /* DW_AT_frame_base */
1906 saa_write8(pinfo,0); /* end of entries */
1907 saalen = pinfo->datalen;
1908 infolen = saalen + 4;
1909 infobuf = pbuf = nasm_malloc(infolen);
1910 WRITELONG(pbuf,saalen); /* initial length */
1911 saa_rnbytes(pinfo, pbuf, saalen);
1912 saa_free(pinfo);
1914 /* build rela.info section */
1915 inforellen = saalen = pinforel->datalen;
1916 inforelbuf = pbuf = nasm_malloc(inforellen);
1917 saa_rnbytes(pinforel, pbuf, saalen);
1918 saa_free(pinforel);
1920 /* build abbrev section */
1921 pabbrev = saa_init(1L);
1922 saa_write8(pabbrev,1); /* entry number LEB128u */
1923 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
1924 saa_write8(pabbrev,1); /* has children */
1925 /* the following attributes and forms are all LEB128u values */
1926 saa_write8(pabbrev,DW_AT_low_pc);
1927 saa_write8(pabbrev,DW_FORM_addr);
1928 saa_write8(pabbrev,DW_AT_high_pc);
1929 saa_write8(pabbrev,DW_FORM_addr);
1930 saa_write8(pabbrev,DW_AT_stmt_list);
1931 saa_write8(pabbrev,DW_FORM_data4);
1932 saa_write8(pabbrev,DW_AT_name);
1933 saa_write8(pabbrev,DW_FORM_string);
1934 saa_write8(pabbrev,DW_AT_producer);
1935 saa_write8(pabbrev,DW_FORM_string);
1936 saa_write8(pabbrev,DW_AT_language);
1937 saa_write8(pabbrev,DW_FORM_data2);
1938 saa_write16(pabbrev,0); /* end of entry */
1939 /* LEB128u usage same as above */
1940 saa_write8(pabbrev,2); /* entry number */
1941 saa_write8(pabbrev,DW_TAG_subprogram);
1942 saa_write8(pabbrev,0); /* no children */
1943 saa_write8(pabbrev,DW_AT_low_pc);
1944 saa_write8(pabbrev,DW_FORM_addr);
1945 saa_write8(pabbrev,DW_AT_frame_base);
1946 saa_write8(pabbrev,DW_FORM_data4);
1947 saa_write16(pabbrev,0); /* end of entry */
1948 abbrevlen = saalen = pabbrev->datalen;
1949 abbrevbuf = pbuf = nasm_malloc(saalen);
1950 saa_rnbytes(pabbrev, pbuf, saalen);
1951 saa_free(pabbrev);
1953 /* build line section */
1954 /* prolog */
1955 plines = saa_init(1L);
1956 saa_write8(plines,1); /* Minimum Instruction Length */
1957 saa_write8(plines,1); /* Initial value of 'is_stmt' */
1958 saa_write8(plines,line_base); /* Line Base */
1959 saa_write8(plines,line_range); /* Line Range */
1960 saa_write8(plines,opcode_base); /* Opcode Base */
1961 /* standard opcode lengths (# of LEB128u operands) */
1962 saa_write8(plines,0); /* Std opcode 1 length */
1963 saa_write8(plines,1); /* Std opcode 2 length */
1964 saa_write8(plines,1); /* Std opcode 3 length */
1965 saa_write8(plines,1); /* Std opcode 4 length */
1966 saa_write8(plines,1); /* Std opcode 5 length */
1967 saa_write8(plines,0); /* Std opcode 6 length */
1968 saa_write8(plines,0); /* Std opcode 7 length */
1969 saa_write8(plines,0); /* Std opcode 8 length */
1970 saa_write8(plines,1); /* Std opcode 9 length */
1971 saa_write8(plines,0); /* Std opcode 10 length */
1972 saa_write8(plines,0); /* Std opcode 11 length */
1973 saa_write8(plines,1); /* Std opcode 12 length */
1974 /* Directory Table */
1975 saa_write8(plines,0); /* End of table */
1976 /* File Name Table */
1977 ftentry = dwarf_flist;
1978 for (indx = 0;indx<dwarf_numfiles;indx++)
1980 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
1981 saa_write8(plines,0); /* directory LEB128u */
1982 saa_write8(plines,0); /* time LEB128u */
1983 saa_write8(plines,0); /* size LEB128u */
1984 ftentry = ftentry->next;
1986 saa_write8(plines,0); /* End of table */
1987 linepoff = plines->datalen;
1988 linelen = linepoff + totlen + 10;
1989 linebuf = pbuf = nasm_malloc(linelen);
1990 WRITELONG(pbuf,linelen-4); /* initial length */
1991 WRITESHORT(pbuf,3); /* dwarf version */
1992 WRITELONG(pbuf,linepoff); /* offset to line number program */
1993 /* write line header */
1994 saalen = linepoff;
1995 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
1996 pbuf += linepoff;
1997 saa_free(plines);
1998 /* concatonate line program ranges */
1999 linepoff += 13;
2000 plinesrel = saa_init(1L);
2001 psect = dwarf_fsect;
2002 for (indx = 0; indx < dwarf_nsections; indx++) {
2003 saa_write32(plinesrel, linepoff);
2004 saa_write32(plinesrel, ((psect->section + 2) << 8) + R_X86_64_32);
2005 saa_write32(plinesrel, 0);
2006 plinep = psect->psaa;
2007 saalen = plinep->datalen;
2008 saa_rnbytes(plinep, pbuf, saalen);
2009 pbuf += saalen;
2010 linepoff += saalen;
2011 saa_free(plinep);
2012 /* done with this entry */
2013 psect = psect->next;
2017 /* build rela.lines section */
2018 linerellen =saalen = plinesrel->datalen;
2019 linerelbuf = pbuf = nasm_malloc(linerellen);
2020 saa_rnbytes(plinesrel, pbuf, saalen);
2021 saa_free(plinesrel);
2023 /* build frame section */
2024 framelen = 4;
2025 framebuf = pbuf = nasm_malloc(framelen);
2026 WRITELONG(pbuf,framelen-4); /* initial length */
2028 /* build loc section */
2029 loclen = 16;
2030 locbuf = pbuf = nasm_malloc(loclen);
2031 WRITELONG(pbuf,0); /* null beginning offset */
2032 WRITELONG(pbuf,0); /* null ending offset */
2035 static void dwarfx32_cleanup(void)
2037 nasm_free(arangesbuf);
2038 nasm_free(arangesrelbuf);
2039 nasm_free(pubnamesbuf);
2040 nasm_free(infobuf);
2041 nasm_free(inforelbuf);
2042 nasm_free(abbrevbuf);
2043 nasm_free(linebuf);
2044 nasm_free(linerelbuf);
2045 nasm_free(framebuf);
2046 nasm_free(locbuf);
2049 static void dwarfx32_findfile(const char * fname)
2051 int finx;
2052 struct linelist *match;
2054 /* return if fname is current file name */
2055 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2056 return;
2058 /* search for match */
2059 match = 0;
2060 if (dwarf_flist) {
2061 match = dwarf_flist;
2062 for (finx = 0; finx < dwarf_numfiles; finx++) {
2063 if (!(strcmp(fname, match->filename))) {
2064 dwarf_clist = match;
2065 return;
2070 /* add file name to end of list */
2071 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2072 dwarf_numfiles++;
2073 dwarf_clist->line = dwarf_numfiles;
2074 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2075 strcpy(dwarf_clist->filename,fname);
2076 dwarf_clist->next = 0;
2077 if (!dwarf_flist) { /* if first entry */
2078 dwarf_flist = dwarf_elist = dwarf_clist;
2079 dwarf_clist->last = 0;
2080 } else { /* chain to previous entry */
2081 dwarf_elist->next = dwarf_clist;
2082 dwarf_elist = dwarf_clist;
2086 static void dwarfx32_findsect(const int index)
2088 int sinx;
2089 struct sectlist *match;
2090 struct SAA *plinep;
2092 /* return if index is current section index */
2093 if (dwarf_csect && (dwarf_csect->section == index))
2094 return;
2096 /* search for match */
2097 match = 0;
2098 if (dwarf_fsect) {
2099 match = dwarf_fsect;
2100 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2101 if (match->section == index) {
2102 dwarf_csect = match;
2103 return;
2105 match = match->next;
2109 /* add entry to end of list */
2110 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2111 dwarf_nsections++;
2112 dwarf_csect->psaa = plinep = saa_init(1L);
2113 dwarf_csect->line = 1;
2114 dwarf_csect->offset = 0;
2115 dwarf_csect->file = 1;
2116 dwarf_csect->section = index;
2117 dwarf_csect->next = 0;
2118 /* set relocatable address at start of line program */
2119 saa_write8(plinep,DW_LNS_extended_op);
2120 saa_write8(plinep,5); /* operand length */
2121 saa_write8(plinep,DW_LNE_set_address);
2122 saa_write32(plinep,0); /* Start Address */
2124 if (!dwarf_fsect) { /* if first entry */
2125 dwarf_fsect = dwarf_esect = dwarf_csect;
2126 dwarf_csect->last = 0;
2127 } else { /* chain to previous entry */
2128 dwarf_esect->next = dwarf_csect;
2129 dwarf_esect = dwarf_csect;
2133 #endif /* OF_ELFX32 */