out: Elfx32 -- Unify dwarf_ types
[nasm.git] / output / outelfx32.c
blobeea9b7fd163f48192d50b0c7eb75f8711b8381c4
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2016 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>
46 #include "nasm.h"
47 #include "nasmlib.h"
48 #include "saa.h"
49 #include "raa.h"
50 #include "stdscan.h"
51 #include "eval.h"
52 #include "output/outform.h"
53 #include "output/outlib.h"
54 #include "rbtree.h"
55 #include "ver.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 const struct ofmt of_elf32;
86 extern const struct ofmt of_elf64;
87 extern const struct ofmt of_elfx32;
89 static struct ELF_SECTDATA {
90 void *data;
91 int64_t len;
92 bool is_saa;
93 } *elf_sects;
94 static int elf_nsect, nsections;
95 static int64_t elf_foffs;
97 static void elf_write(void);
98 static void elf_sect_write(struct elf_section *, const void *, size_t);
99 static void elf_sect_writeaddr(struct elf_section *, int64_t, size_t);
100 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
101 int, int);
102 static void elf_write_sections(void);
103 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
104 static struct SAA *elf_build_reltab(uint64_t *, struct elf_reloc *);
105 static void add_sectname(char *, char *);
107 struct erel {
108 int offset, info;
111 struct symlininfo {
112 int offset;
113 int section; /* index into sects[] */
114 int segto; /* internal section number */
115 char *name; /* shallow-copied pointer of section name */
118 struct linelist {
119 struct linelist *next;
120 struct linelist *last;
121 struct symlininfo info;
122 char *filename;
123 int line;
126 struct sectlist {
127 struct SAA *psaa;
128 int section;
129 int line;
130 int offset;
131 int file;
132 struct sectlist *next;
133 struct sectlist *last;
136 /* common debug variables */
137 static int currentline = 1;
138 static int debug_immcall = 0;
140 /* stabs debug variables */
141 static struct linelist *stabslines = 0;
142 static int numlinestabs = 0;
143 static char *stabs_filename = 0;
144 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
145 static int stablen, stabstrlen, stabrellen;
147 /* dwarf debug variables */
148 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
149 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
150 static int dwarf_numfiles = 0, dwarf_nsections;
151 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
152 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
153 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
154 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
155 abbrevlen, linelen, linerellen, framelen, loclen;
156 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
158 static const struct dfmt df_dwarf;
159 static const struct dfmt df_stabs;
160 static struct elf_symbol *lastsym;
162 /* common debugging routines */
163 static void debug_typevalue(int32_t);
165 /* stabs debugging routines */
166 static void stabs_linenum(const char *filename, int32_t linenumber, int32_t);
167 static void stabs_output(int, void *);
168 static void stabs_generate(void);
169 static void stabs_cleanup(void);
171 /* dwarf debugging routines */
172 static void dwarf_init(void);
173 static void dwarf_linenum(const char *filename, int32_t linenumber, int32_t);
174 static void dwarf_output(int, void *);
175 static void dwarf_generate(void);
176 static void dwarf_cleanup(void);
177 static void dwarf_findfile(const char *);
178 static void dwarf_findsect(const int);
181 * Special NASM section numbers which are used to define ELF special
182 * symbols.
184 static int32_t elf_gotpc_sect, elf_gotoff_sect;
185 static int32_t elf_got_sect, elf_plt_sect;
186 static int32_t elf_sym_sect, elf_gottpoff_sect, elf_tlsie_sect;
188 static void elf_init(void)
190 sects = NULL;
191 nsects = sectlen = 0;
192 syms = saa_init((int32_t)sizeof(struct elf_symbol));
193 nlocals = nglobs = ndebugs = 0;
194 bsym = raa_init();
195 strs = saa_init(1L);
196 saa_wbytes(strs, "\0", 1L);
197 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
198 strslen = 2 + strlen(elf_module);
199 shstrtab = NULL;
200 shstrtablen = shstrtabsize = 0;;
201 add_sectname("", "");
203 fwds = NULL;
206 * FIXME: tlsie is Elf32 only and
207 * gottpoff is Elfx32|64 only.
210 elf_gotpc_sect = seg_alloc();
211 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
212 elf_gotoff_sect = seg_alloc();
213 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
214 elf_got_sect = seg_alloc();
215 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
216 elf_plt_sect = seg_alloc();
217 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
218 elf_sym_sect = seg_alloc();
219 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
220 elf_gottpoff_sect = seg_alloc();
221 define_label("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false);
222 elf_tlsie_sect = seg_alloc();
223 define_label("..tlsie", elf_tlsie_sect + 1, 0L, NULL, false, false);
225 def_seg = seg_alloc();
228 static void elf_cleanup(void)
230 struct elf_reloc *r;
231 int i;
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 dfmt->cleanup();
252 /* add entry to the elf .shstrtab section */
253 static void add_sectname(char *firsthalf, char *secondhalf)
255 int len = strlen(firsthalf) + strlen(secondhalf);
256 while (shstrtablen + len + 1 > shstrtabsize)
257 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
258 strcpy(shstrtab + shstrtablen, firsthalf);
259 strcat(shstrtab + shstrtablen, secondhalf);
260 shstrtablen += len + 1;
263 static int elf_make_section(char *name, int type, int flags, int align)
265 struct elf_section *s;
267 s = nasm_zalloc(sizeof(*s));
269 if (type != SHT_NOBITS)
270 s->data = saa_init(1L);
271 s->tail = &s->head;
272 if (!strcmp(name, ".text"))
273 s->index = def_seg;
274 else
275 s->index = seg_alloc();
276 add_sectname("", name);
278 s->name = nasm_strdup(name);
279 s->type = type;
280 s->flags = flags;
281 s->align = align;
283 if (nsects >= sectlen)
284 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
285 sects[nsects++] = s;
287 return nsects - 1;
290 static int32_t elf_section_names(char *name, int pass, int *bits)
292 char *p;
293 uint32_t flags, flags_and, flags_or;
294 uint64_t align;
295 int type, i;
297 if (!name) {
298 *bits = ofmt->maxbits;
299 return def_seg;
302 p = nasm_skip_word(name);
303 if (*p)
304 *p++ = '\0';
305 flags_and = flags_or = type = align = 0;
307 elf_section_attrib(name, p, pass, &flags_and,
308 &flags_or, &align, &type);
310 if (!strcmp(name, ".shstrtab") ||
311 !strcmp(name, ".symtab") ||
312 !strcmp(name, ".strtab")) {
313 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
314 "name `%s'", name);
315 return NO_SEG;
318 for (i = 0; i < nsects; i++)
319 if (!strcmp(name, sects[i]->name))
320 break;
321 if (i == nsects) {
322 const struct elf_known_section *ks = elf_known_sections;
324 while (ks->name) {
325 if (!strcmp(name, ks->name))
326 break;
327 ks++;
330 type = type ? type : ks->type;
331 align = align ? align : ks->align;
332 flags = (ks->flags & ~flags_and) | flags_or;
334 i = elf_make_section(name, type, flags, align);
335 } else if (pass == 1) {
336 if ((type && sects[i]->type != type)
337 || (align && sects[i]->align != align)
338 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
339 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
340 " redeclaration of section `%s'", name);
343 return sects[i]->index;
346 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
347 int is_global, char *special)
349 int pos = strslen;
350 struct elf_symbol *sym;
351 bool special_used = false;
353 #if defined(DEBUG) && DEBUG>2
354 nasm_error(ERR_DEBUG,
355 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
356 name, segment, offset, is_global, special);
357 #endif
358 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
360 * This is a NASM special symbol. We never allow it into
361 * the ELF symbol table, even if it's a valid one. If it
362 * _isn't_ a valid one, we should barf immediately.
364 * FIXME: tlsie is Elf32 only, and gottpoff is Elfx32|64 only.
366 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
367 strcmp(name, "..got") && strcmp(name, "..plt") &&
368 strcmp(name, "..sym") && strcmp(name, "..gottpoff") &&
369 strcmp(name, "..tlsie"))
370 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
371 return;
374 if (is_global == 3) {
375 struct elf_symbol **s;
377 * Fix up a forward-reference symbol size from the first
378 * pass.
380 for (s = &fwds; *s; s = &(*s)->nextfwd)
381 if (!strcmp((*s)->name, name)) {
382 struct tokenval tokval;
383 expr *e;
384 char *p = nasm_skip_spaces(nasm_skip_word(special));
386 stdscan_reset();
387 stdscan_set(p);
388 tokval.t_type = TOKEN_INVALID;
389 e = evaluate(stdscan, NULL, &tokval, NULL, 1, NULL);
390 if (e) {
391 if (!is_simple(e))
392 nasm_error(ERR_NONFATAL, "cannot use relocatable"
393 " expression as symbol size");
394 else
395 (*s)->size = reloc_value(e);
399 * Remove it from the list of unresolved sizes.
401 nasm_free((*s)->name);
402 *s = (*s)->nextfwd;
403 return;
405 return; /* it wasn't an important one */
408 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
409 strslen += 1 + strlen(name);
411 lastsym = sym = saa_wstruct(syms);
413 memset(&sym->symv, 0, sizeof(struct rbtree));
415 sym->strpos = pos;
416 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
417 sym->other = STV_DEFAULT;
418 sym->size = 0;
419 if (segment == NO_SEG)
420 sym->section = SHN_ABS;
421 else {
422 int i;
423 sym->section = SHN_UNDEF;
424 if (segment == def_seg) {
425 /* we have to be sure at least text section is there */
426 int tempint;
427 if (segment != elf_section_names(".text", 2, &tempint))
428 nasm_panic(0, "strange segment conditions in ELF driver");
430 for (i = 0; i < nsects; i++) {
431 if (segment == sects[i]->index) {
432 sym->section = i + 1;
433 break;
438 if (is_global == 2) {
439 sym->size = offset;
440 sym->symv.key = 0;
441 sym->section = SHN_COMMON;
443 * We have a common variable. Check the special text to see
444 * if it's a valid number and power of two; if so, store it
445 * as the alignment for the common variable.
447 if (special) {
448 bool err;
449 sym->symv.key = readnum(special, &err);
450 if (err)
451 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
452 " valid number", special);
453 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
454 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
455 " power of two", special);
457 special_used = true;
458 } else
459 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
461 if (sym->type == SYM_GLOBAL) {
463 * If sym->section == SHN_ABS, then the first line of the
464 * else section would cause a core dump, because its a reference
465 * beyond the end of the section array.
466 * This behaviour is exhibited by this code:
467 * GLOBAL crash_nasm
468 * crash_nasm equ 0
469 * To avoid such a crash, such requests are silently discarded.
470 * This may not be the best solution.
472 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
473 bsym = raa_write(bsym, segment, nglobs);
474 } else if (sym->section != SHN_ABS) {
476 * This is a global symbol; so we must add it to the rbtree
477 * of global symbols in its section.
479 * In addition, we check the special text for symbol
480 * type and size information.
482 sects[sym->section-1]->gsyms =
483 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
485 if (special) {
486 int n = strcspn(special, " \t");
488 if (!nasm_strnicmp(special, "function", n))
489 sym->type |= STT_FUNC;
490 else if (!nasm_strnicmp(special, "data", n) ||
491 !nasm_strnicmp(special, "object", n))
492 sym->type |= STT_OBJECT;
493 else if (!nasm_strnicmp(special, "notype", n))
494 sym->type |= STT_NOTYPE;
495 else
496 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
497 n, special);
498 special += n;
500 special = nasm_skip_spaces(special);
501 if (*special) {
502 n = strcspn(special, " \t");
503 if (!nasm_strnicmp(special, "default", n))
504 sym->other = STV_DEFAULT;
505 else if (!nasm_strnicmp(special, "internal", n))
506 sym->other = STV_INTERNAL;
507 else if (!nasm_strnicmp(special, "hidden", n))
508 sym->other = STV_HIDDEN;
509 else if (!nasm_strnicmp(special, "protected", n))
510 sym->other = STV_PROTECTED;
511 else
512 n = 0;
513 special += n;
516 if (*special) {
517 struct tokenval tokval;
518 expr *e;
519 int fwd = 0;
520 char *saveme = stdscan_get();
522 while (special[n] && nasm_isspace(special[n]))
523 n++;
525 * We have a size expression; attempt to
526 * evaluate it.
528 stdscan_reset();
529 stdscan_set(special + n);
530 tokval.t_type = TOKEN_INVALID;
531 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, NULL);
532 if (fwd) {
533 sym->nextfwd = fwds;
534 fwds = sym;
535 sym->name = nasm_strdup(name);
536 } else if (e) {
537 if (!is_simple(e))
538 nasm_error(ERR_NONFATAL, "cannot use relocatable"
539 " expression as symbol size");
540 else
541 sym->size = reloc_value(e);
543 stdscan_set(saveme);
545 special_used = true;
548 * If TLS segment, mark symbol accordingly.
550 if (sects[sym->section - 1]->flags & SHF_TLS) {
551 sym->type &= 0xf0;
552 sym->type |= STT_TLS;
555 sym->globnum = nglobs;
556 nglobs++;
557 } else
558 nlocals++;
560 if (special && !special_used)
561 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
564 static void elf_add_reloc(struct elf_section *sect, int32_t segment,
565 int64_t offset, int type)
567 struct elf_reloc *r;
569 r = *sect->tail = nasm_zalloc(sizeof(struct elf_reloc));
570 sect->tail = &r->next;
572 r->address = sect->len;
573 r->offset = offset;
575 if (segment != NO_SEG) {
576 int i;
577 for (i = 0; i < nsects; i++)
578 if (segment == sects[i]->index)
579 r->symbol = i + 2;
580 if (!r->symbol)
581 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
583 r->type = type;
585 sect->nrelocs++;
589 * This routine deals with ..got and ..sym relocations: the more
590 * complicated kinds. In shared-library writing, some relocations
591 * with respect to global symbols must refer to the precise symbol
592 * rather than referring to an offset from the base of the section
593 * _containing_ the symbol. Such relocations call to this routine,
594 * which searches the symbol list for the symbol in question.
596 * R_X86_64_GOT32 references require the _exact_ symbol address to be
597 * used; R_X86_64_32 references can be at an offset from the symbol.
598 * The boolean argument `exact' tells us this.
600 * Return value is the adjusted value of `addr', having become an
601 * offset from the symbol rather than the section. Should always be
602 * zero when returning from an exact call.
604 * Limitation: if you define two symbols at the same place,
605 * confusion will occur.
607 * Inefficiency: we search, currently, using a linked list which
608 * isn't even necessarily sorted.
610 static int64_t elf_add_gsym_reloc(struct elf_section *sect,
611 int32_t segment, uint64_t offset, int64_t pcrel,
612 int type, bool exact)
614 struct elf_reloc *r;
615 struct elf_section *s;
616 struct elf_symbol *sym;
617 struct rbtree *srb;
618 int i;
621 * First look up the segment/offset pair and find a global
622 * symbol corresponding to it. If it's not one of our segments,
623 * then it must be an external symbol, in which case we're fine
624 * doing a normal elf_add_reloc after first sanity-checking
625 * that the offset from the symbol is zero.
627 s = NULL;
628 for (i = 0; i < nsects; i++)
629 if (segment == sects[i]->index) {
630 s = sects[i];
631 break;
634 if (!s) {
635 if (exact && offset)
636 nasm_error(ERR_NONFATAL, "invalid access to an external symbol");
637 else
638 elf_add_reloc(sect, segment, offset - pcrel, type);
639 return 0;
642 srb = rb_search(s->gsyms, offset);
643 if (!srb || (exact && srb->key != offset)) {
644 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
645 " for this reference");
646 return 0;
648 sym = container_of(srb, struct elf_symbol, symv);
650 r = *sect->tail = nasm_malloc(sizeof(struct elf_reloc));
651 sect->tail = &r->next;
653 r->next = NULL;
654 r->address = sect->len;
655 r->offset = offset - pcrel - sym->symv.key;
656 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
657 r->type = type;
659 sect->nrelocs++;
660 return r->offset;
663 static void elf_out(int32_t segto, const void *data,
664 enum out_type type, uint64_t size,
665 int32_t segment, int32_t wrt)
667 struct elf_section *s;
668 int64_t addr;
669 int reltype, bytes;
670 int i;
671 static struct symlininfo sinfo;
674 * handle absolute-assembly (structure definitions)
676 if (segto == NO_SEG) {
677 if (type != OUT_RESERVE)
678 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
679 " space");
680 return;
683 s = NULL;
684 for (i = 0; i < nsects; i++)
685 if (segto == sects[i]->index) {
686 s = sects[i];
687 break;
689 if (!s) {
690 int tempint; /* ignored */
691 if (segto != elf_section_names(".text", 2, &tempint))
692 nasm_panic(0, "strange segment conditions in ELF driver");
693 else {
694 s = sects[nsects - 1];
695 i = nsects - 1;
699 /* again some stabs debugging stuff */
700 sinfo.offset = s->len;
701 sinfo.section = i;
702 sinfo.segto = segto;
703 sinfo.name = s->name;
704 dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
705 /* end of debugging stuff */
707 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
708 nasm_error(ERR_WARNING, "attempt to initialize memory in"
709 " BSS section `%s': ignored", s->name);
710 s->len += realsize(type, size);
711 return;
714 switch (type) {
715 case OUT_RESERVE:
716 if (s->type == SHT_PROGBITS) {
717 nasm_error(ERR_WARNING, "uninitialized space declared in"
718 " non-BSS section `%s': zeroing", s->name);
719 elf_sect_write(s, NULL, size);
720 } else
721 s->len += size;
722 break;
724 case OUT_RAWDATA:
725 if (segment != NO_SEG)
726 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
727 elf_sect_write(s, data, size);
728 break;
730 case OUT_ADDRESS:
732 int isize = (int)size;
733 int asize = abs((int)size);
735 addr = *(int64_t *)data;
736 if (segment == NO_SEG) {
737 /* Do nothing */
738 } else if (segment % 2) {
739 nasm_error(ERR_NONFATAL, "ELF format does not support"
740 " segment base references");
741 } else {
742 if (wrt == NO_SEG) {
743 switch (isize) {
744 case 1:
745 case -1:
746 elf_add_reloc(s, segment, addr, R_X86_64_8);
747 break;
748 case 2:
749 case -2:
750 elf_add_reloc(s, segment, addr, R_X86_64_16);
751 break;
752 case 4:
753 elf_add_reloc(s, segment, addr, R_X86_64_32);
754 break;
755 case -4:
756 elf_add_reloc(s, segment, addr, R_X86_64_32S);
757 break;
758 case 8:
759 case -8:
760 elf_add_reloc(s, segment, addr, R_X86_64_64);
761 break;
762 default:
763 nasm_panic(0, "internal error elfx32-hpa-871");
764 break;
766 addr = 0;
767 } else if (wrt == elf_gotpc_sect + 1) {
769 * The user will supply GOT relative to $$. ELF
770 * will let us have GOT relative to $. So we
771 * need to fix up the data item by $-$$.
773 addr += s->len;
774 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
775 addr = 0;
776 } else if (wrt == elf_gotoff_sect + 1) {
777 nasm_error(ERR_NONFATAL, "ELFX32 doesn't support "
778 "R_X86_64_GOTOFF64");
779 } else if (wrt == elf_got_sect + 1) {
780 switch (asize) {
781 case 4:
782 elf_add_gsym_reloc(s, segment, addr, 0,
783 R_X86_64_GOT32, true);
784 addr = 0;
785 break;
786 default:
787 nasm_error(ERR_NONFATAL, "invalid ..got reference");
788 break;
790 } else if (wrt == elf_sym_sect + 1) {
791 switch (isize) {
792 case 1:
793 case -1:
794 elf_add_gsym_reloc(s, segment, addr, 0,
795 R_X86_64_8, false);
796 addr = 0;
797 break;
798 case 2:
799 case -2:
800 elf_add_gsym_reloc(s, segment, addr, 0,
801 R_X86_64_16, false);
802 addr = 0;
803 break;
804 case 4:
805 elf_add_gsym_reloc(s, segment, addr, 0,
806 R_X86_64_32, false);
807 addr = 0;
808 break;
809 case -4:
810 elf_add_gsym_reloc(s, segment, addr, 0,
811 R_X86_64_32S, false);
812 addr = 0;
813 break;
814 case 8:
815 case -8:
816 elf_add_gsym_reloc(s, segment, addr, 0,
817 R_X86_64_64, false);
818 addr = 0;
819 break;
820 default:
821 nasm_panic(0, "internal error elfx32-hpa-903");
822 break;
824 } else if (wrt == elf_plt_sect + 1) {
825 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
826 "relative PLT references");
827 } else {
828 nasm_error(ERR_NONFATAL, "ELF format does not support this"
829 " use of WRT");
832 elf_sect_writeaddr(s, addr, asize);
833 break;
836 case OUT_REL1ADR:
837 reltype = R_X86_64_PC8;
838 bytes = 1;
839 goto rel12adr;
841 case OUT_REL2ADR:
842 reltype = R_X86_64_PC16;
843 bytes = 2;
844 goto rel12adr;
846 rel12adr:
847 addr = *(int64_t *)data - size;
848 if (segment == segto)
849 nasm_panic(0, "intra-segment OUT_REL1ADR");
850 if (segment == NO_SEG) {
851 /* Do nothing */
852 } else if (segment % 2) {
853 nasm_error(ERR_NONFATAL, "ELF format does not support"
854 " segment base references");
855 } else {
856 if (wrt == NO_SEG) {
857 elf_add_reloc(s, segment, addr, reltype);
858 addr = 0;
859 } else {
860 nasm_error(ERR_NONFATAL,
861 "Unsupported non-32-bit ELF relocation");
864 elf_sect_writeaddr(s, addr, bytes);
865 break;
867 case OUT_REL4ADR:
868 addr = *(int64_t *)data - size;
869 if (segment == segto)
870 nasm_panic(0, "intra-segment OUT_REL4ADR");
871 if (segment == NO_SEG) {
872 /* Do nothing */
873 } else if (segment % 2) {
874 nasm_error(ERR_NONFATAL, "ELFX32 format does not support"
875 " segment base references");
876 } else {
877 if (wrt == NO_SEG) {
878 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
879 addr = 0;
880 } else if (wrt == elf_plt_sect + 1) {
881 elf_add_gsym_reloc(s, segment, addr+size, size,
882 R_X86_64_PLT32, true);
883 addr = 0;
884 } else if (wrt == elf_gotpc_sect + 1 ||
885 wrt == elf_got_sect + 1) {
886 elf_add_gsym_reloc(s, segment, addr+size, size,
887 R_X86_64_GOTPCREL, true);
888 addr = 0;
889 } else if (wrt == elf_gotoff_sect + 1 ||
890 wrt == elf_got_sect + 1) {
891 nasm_error(ERR_NONFATAL, "invalid ..gotoff reference");
892 } else if (wrt == elf_gottpoff_sect + 1) {
893 elf_add_gsym_reloc(s, segment, addr+size, size,
894 R_X86_64_GOTTPOFF, true);
895 addr = 0;
896 } else {
897 nasm_error(ERR_NONFATAL, "ELFX32 format does not support this"
898 " use of WRT");
901 elf_sect_writeaddr(s, addr, 4);
902 break;
904 case OUT_REL8ADR:
905 nasm_error(ERR_NONFATAL,
906 "32-bit ELF format does not support 64-bit relocations");
907 addr = 0;
908 elf_sect_writeaddr(s, addr, 8);
909 break;
913 static void elf_write(void)
915 int align;
916 char *p;
917 int i;
919 struct SAA *symtab;
920 int32_t symtablen, symtablocal;
923 * Work out how many sections we will have. We have SHN_UNDEF,
924 * then the flexible user sections, then the fixed sections
925 * `.shstrtab', `.symtab' and `.strtab', then optionally
926 * relocation sections for the user sections.
928 nsections = sec_numspecial + 1;
929 if (dfmt == &df_stabs)
930 nsections += 3;
931 else if (dfmt == &df_dwarf)
932 nsections += 10;
934 add_sectname("", ".shstrtab");
935 add_sectname("", ".symtab");
936 add_sectname("", ".strtab");
937 for (i = 0; i < nsects; i++) {
938 nsections++; /* for the section itself */
939 if (sects[i]->head) {
940 nsections++; /* for its relocations */
941 add_sectname(".rela", sects[i]->name);
945 if (dfmt == &df_stabs) {
946 /* in case the debug information is wanted, just add these three sections... */
947 add_sectname("", ".stab");
948 add_sectname("", ".stabstr");
949 add_sectname(".rel", ".stab");
950 } else if (dfmt == &df_dwarf) {
951 /* the dwarf debug standard specifies the following ten sections,
952 not all of which are currently implemented,
953 although all of them are defined. */
954 add_sectname("", ".debug_aranges");
955 add_sectname(".rela", ".debug_aranges");
956 add_sectname("", ".debug_pubnames");
957 add_sectname("", ".debug_info");
958 add_sectname(".rela", ".debug_info");
959 add_sectname("", ".debug_abbrev");
960 add_sectname("", ".debug_line");
961 add_sectname(".rela", ".debug_line");
962 add_sectname("", ".debug_frame");
963 add_sectname("", ".debug_loc");
967 * Output the ELF header.
969 nasm_write("\177ELF\1\1\1", 7, ofile);
970 fputc(elf_osabi, ofile);
971 fputc(elf_abiver, ofile);
972 fwritezero(7, ofile);
973 fwriteint16_t(ET_REL, ofile); /* relocatable file */
974 fwriteint16_t(EM_X86_64, ofile); /* processor ID */
975 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
976 fwriteint32_t(0L, ofile); /* no entry point */
977 fwriteint32_t(0L, ofile); /* no program header table */
978 fwriteint32_t(0x40L, ofile); /* section headers straight after
979 * ELF header plus alignment */
980 fwriteint32_t(0L, ofile); /* X86_64 defines no special flags */
981 fwriteint16_t(0x34, ofile); /* size of ELF header */
982 fwriteint16_t(0, ofile); /* no program header table, again */
983 fwriteint16_t(0, ofile); /* still no program header table */
984 fwriteint16_t(sizeof(Elf32_Shdr), ofile); /* size of section header */
985 fwriteint16_t(nsections, ofile); /* number of sections */
986 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
987 * section header table */
988 fwriteint32_t(0L, ofile); /* align to 0x40 bytes */
989 fwriteint32_t(0L, ofile);
990 fwriteint32_t(0L, ofile);
993 * Build the symbol table and relocation tables.
995 symtab = elf_build_symtab(&symtablen, &symtablocal);
996 for (i = 0; i < nsects; i++)
997 if (sects[i]->head)
998 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
999 sects[i]->head);
1002 * Now output the section header table.
1005 elf_foffs = 0x40 + sizeof(Elf32_Shdr) * nsections;
1006 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1007 elf_foffs += align;
1008 elf_nsect = 0;
1009 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1011 /* SHN_UNDEF */
1012 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1013 p = shstrtab + 1;
1015 /* The normal sections */
1016 for (i = 0; i < nsects; i++) {
1017 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1018 (sects[i]->type == SHT_PROGBITS ?
1019 sects[i]->data : NULL), true,
1020 sects[i]->len, 0, 0, sects[i]->align, 0);
1021 p += strlen(p) + 1;
1024 /* .shstrtab */
1025 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1026 shstrtablen, 0, 0, 1, 0);
1027 p += strlen(p) + 1;
1029 /* .symtab */
1030 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1031 symtablen, sec_strtab, symtablocal, 4, 16);
1032 p += strlen(p) + 1;
1034 /* .strtab */
1035 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1036 strslen, 0, 0, 1, 0);
1037 p += strlen(p) + 1;
1039 /* The relocation sections */
1040 for (i = 0; i < nsects; i++)
1041 if (sects[i]->head) {
1042 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1043 sects[i]->rellen, sec_symtab, i + 1, 4, 12);
1044 p += strlen(p) + 1;
1047 if (dfmt == &df_stabs) {
1048 /* for debugging information, create the last three sections
1049 which are the .stab , .stabstr and .rel.stab sections respectively */
1051 /* this function call creates the stab sections in memory */
1052 stabs_generate();
1054 if (stabbuf && stabstrbuf && stabrelbuf) {
1055 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1056 stablen, sec_stabstr, 0, 4, 12);
1057 p += strlen(p) + 1;
1059 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1060 stabstrlen, 0, 0, 4, 0);
1061 p += strlen(p) + 1;
1063 /* link -> symtable info -> section to refer to */
1064 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1065 stabrellen, sec_symtab, sec_stab, 4, 8);
1066 p += strlen(p) + 1;
1068 } else if (dfmt == &df_dwarf) {
1069 /* for dwarf debugging information, create the ten dwarf sections */
1071 /* this function call creates the dwarf sections in memory */
1072 if (dwarf_fsect)
1073 dwarf_generate();
1075 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1076 arangeslen, 0, 0, 1, 0);
1077 p += strlen(p) + 1;
1079 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1080 arangesrellen, sec_symtab, sec_debug_aranges, 1, 12);
1081 p += strlen(p) + 1;
1083 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1084 pubnameslen, 0, 0, 1, 0);
1085 p += strlen(p) + 1;
1087 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1088 infolen, 0, 0, 1, 0);
1089 p += strlen(p) + 1;
1091 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1092 inforellen, sec_symtab, sec_debug_info, 1, 12);
1093 p += strlen(p) + 1;
1095 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1096 abbrevlen, 0, 0, 1, 0);
1097 p += strlen(p) + 1;
1099 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1100 linelen, 0, 0, 1, 0);
1101 p += strlen(p) + 1;
1103 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1104 linerellen, sec_symtab, sec_debug_line, 1, 12);
1105 p += strlen(p) + 1;
1107 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1108 framelen, 0, 0, 8, 0);
1109 p += strlen(p) + 1;
1111 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1112 loclen, 0, 0, 1, 0);
1113 p += strlen(p) + 1;
1115 fwritezero(align, ofile);
1118 * Now output the sections.
1120 elf_write_sections();
1122 nasm_free(elf_sects);
1123 saa_free(symtab);
1126 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1128 struct SAA *s = saa_init(1L);
1129 struct elf_symbol *sym;
1130 uint8_t entry[24], *p;
1131 int i;
1133 *len = *local = 0;
1136 * First, an all-zeros entry, required by the ELF spec.
1138 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1139 *len += 16;
1140 (*local)++;
1143 * Next, an entry for the file name.
1145 p = entry;
1146 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1147 WRITELONG(p, 0); /* no value */
1148 WRITELONG(p, 0); /* no size either */
1149 WRITESHORT(p, STT_FILE); /* type FILE */
1150 WRITESHORT(p, SHN_ABS);
1151 saa_wbytes(s, entry, 16L);
1152 *len += 16;
1153 (*local)++;
1156 * Now some standard symbols defining the segments, for relocation
1157 * purposes.
1159 for (i = 1; i <= nsects; i++) {
1160 p = entry;
1161 WRITELONG(p, 0); /* no symbol name */
1162 WRITELONG(p, 0); /* offset zero */
1163 WRITELONG(p, 0); /* size zero */
1164 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1165 WRITESHORT(p, i); /* section id */
1166 saa_wbytes(s, entry, 16L);
1167 *len += 16;
1168 (*local)++;
1172 * Now the other local symbols.
1174 saa_rewind(syms);
1175 while ((sym = saa_rstruct(syms))) {
1176 if (sym->type & SYM_GLOBAL)
1177 continue;
1178 p = entry;
1179 WRITELONG(p, sym->strpos); /* index into symbol string table */
1180 WRITELONG(p, sym->symv.key); /* value of symbol */
1181 WRITELONG(p, sym->size); /* size of symbol */
1182 WRITECHAR(p, sym->type); /* type and binding */
1183 WRITECHAR(p, sym->other); /* visibility */
1184 WRITESHORT(p, sym->section); /* index into section header table */
1185 saa_wbytes(s, entry, 16L);
1186 *len += 16;
1187 (*local)++;
1190 * dwarf needs symbols for debug sections
1191 * which are relocation targets.
1193 if (dfmt == &df_dwarf) {
1194 dwarf_infosym = *local;
1195 p = entry;
1196 WRITELONG(p, 0); /* no symbol name */
1197 WRITELONG(p, 0); /* offset zero */
1198 WRITELONG(p, 0); /* size zero */
1199 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1200 WRITESHORT(p, sec_debug_info); /* section id */
1201 saa_wbytes(s, entry, 16L);
1202 *len += 16;
1203 (*local)++;
1204 dwarf_abbrevsym = *local;
1205 p = entry;
1206 WRITELONG(p, 0); /* no symbol name */
1207 WRITELONG(p, 0); /* offset zero */
1208 WRITELONG(p, 0); /* size zero */
1209 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1210 WRITESHORT(p, sec_debug_abbrev); /* section id */
1211 saa_wbytes(s, entry, 16L);
1212 *len += 16;
1213 (*local)++;
1214 dwarf_linesym = *local;
1215 p = entry;
1216 WRITELONG(p, 0); /* no symbol name */
1217 WRITELONG(p, 0); /* offset zero */
1218 WRITELONG(p, 0); /* size zero */
1219 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1220 WRITESHORT(p, sec_debug_line); /* section id */
1221 saa_wbytes(s, entry, 16L);
1222 *len += 16;
1223 (*local)++;
1227 * Now the global symbols.
1229 saa_rewind(syms);
1230 while ((sym = saa_rstruct(syms))) {
1231 if (!(sym->type & SYM_GLOBAL))
1232 continue;
1233 p = entry;
1234 WRITELONG(p, sym->strpos);
1235 WRITELONG(p, sym->symv.key);
1236 WRITELONG(p, sym->size);
1237 WRITECHAR(p, sym->type); /* type and binding */
1238 WRITECHAR(p, sym->other); /* visibility */
1239 WRITESHORT(p, sym->section);
1240 saa_wbytes(s, entry, 16L);
1241 *len += 16;
1244 return s;
1247 static struct SAA *elf_build_reltab(uint64_t *len, struct elf_reloc *r)
1249 struct SAA *s;
1250 uint8_t *p, entry[12];
1251 int32_t global_offset;
1253 if (!r)
1254 return NULL;
1256 s = saa_init(1L);
1257 *len = 0;
1260 * How to onvert from a global placeholder to a real symbol index;
1261 * the +2 refers to the two special entries, the null entry and
1262 * the filename entry.
1264 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1266 while (r) {
1267 int32_t sym = r->symbol;
1269 if (sym >= GLOBAL_TEMP_BASE)
1270 sym += global_offset;
1272 p = entry;
1273 WRITELONG(p, r->address);
1274 WRITELONG(p, (sym << 8) + r->type);
1275 WRITELONG(p, r->offset);
1276 saa_wbytes(s, entry, 12L);
1277 *len += 12;
1279 r = r->next;
1282 return s;
1285 static void elf_section_header(int name, int type, uint64_t flags,
1286 void *data, bool is_saa, uint64_t datalen,
1287 int link, int info, int align, int eltsize)
1289 elf_sects[elf_nsect].data = data;
1290 elf_sects[elf_nsect].len = datalen;
1291 elf_sects[elf_nsect].is_saa = is_saa;
1292 elf_nsect++;
1294 fwriteint32_t((int32_t)name, ofile);
1295 fwriteint32_t((int32_t)type, ofile);
1296 fwriteint32_t((int32_t)flags, ofile);
1297 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
1298 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
1299 fwriteint32_t(datalen, ofile);
1300 if (data)
1301 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
1302 fwriteint32_t((int32_t)link, ofile);
1303 fwriteint32_t((int32_t)info, ofile);
1304 fwriteint32_t((int32_t)align, ofile);
1305 fwriteint32_t((int32_t)eltsize, ofile);
1308 static void elf_write_sections(void)
1310 int i;
1311 for (i = 0; i < elf_nsect; i++)
1312 if (elf_sects[i].data) {
1313 int32_t len = elf_sects[i].len;
1314 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
1315 int32_t align = reallen - len;
1316 if (elf_sects[i].is_saa)
1317 saa_fpwrite(elf_sects[i].data, ofile);
1318 else
1319 nasm_write(elf_sects[i].data, len, ofile);
1320 fwritezero(align, ofile);
1324 static void elf_sect_write(struct elf_section *sect, const void *data, size_t len)
1326 saa_wbytes(sect->data, data, len);
1327 sect->len += len;
1330 static void elf_sect_writeaddr(struct elf_section *sect, int64_t data, size_t len)
1332 saa_writeaddr(sect->data, data, len);
1333 sect->len += len;
1336 static void elf_sectalign(int32_t seg, unsigned int value)
1338 struct elf_section *s = NULL;
1339 int i;
1341 for (i = 0; i < nsects; i++) {
1342 if (sects[i]->index == seg) {
1343 s = sects[i];
1344 break;
1347 if (!s || !is_power2(value))
1348 return;
1350 if (value > s->align)
1351 s->align = value;
1354 static int32_t elf_segbase(int32_t segment)
1356 return segment;
1359 static void elf_filename(char *inname, char *outname)
1361 strcpy(elf_module, inname);
1362 standard_extension(inname, outname, ".o");
1365 extern macros_t elf_stdmac[];
1367 static int elf_set_info(enum geninfo type, char **val)
1369 (void)type;
1370 (void)val;
1371 return 0;
1373 static const struct dfmt df_dwarf = {
1374 "ELFX32 (x86-64) dwarf debug format for Linux/Unix",
1375 "dwarf",
1376 dwarf_init,
1377 dwarf_linenum,
1378 null_debug_deflabel,
1379 null_debug_directive,
1380 debug_typevalue,
1381 dwarf_output,
1382 dwarf_cleanup
1384 static const struct dfmt df_stabs = {
1385 "ELFX32 (x86-64) stabs debug format for Linux/Unix",
1386 "stabs",
1387 null_debug_init,
1388 stabs_linenum,
1389 null_debug_deflabel,
1390 null_debug_directive,
1391 debug_typevalue,
1392 stabs_output,
1393 stabs_cleanup
1396 static const struct dfmt * const elfx32_debugs_arr[3] =
1397 { &df_dwarf, &df_stabs, NULL };
1399 const struct ofmt of_elfx32 = {
1400 "ELFX32 (x86_64) object files (e.g. Linux)",
1401 "elfx32",
1404 elfx32_debugs_arr,
1405 &df_stabs,
1406 elf_stdmac,
1407 elf_init,
1408 elf_set_info,
1409 elf_out,
1410 elf_deflabel,
1411 elf_section_names,
1412 elf_sectalign,
1413 elf_segbase,
1414 elf_directive,
1415 elf_filename,
1416 elf_cleanup
1419 /* common debugging routines */
1420 static void debug_typevalue(int32_t type)
1422 int32_t stype, ssize;
1423 switch (TYM_TYPE(type)) {
1424 case TY_LABEL:
1425 ssize = 0;
1426 stype = STT_NOTYPE;
1427 break;
1428 case TY_BYTE:
1429 ssize = 1;
1430 stype = STT_OBJECT;
1431 break;
1432 case TY_WORD:
1433 ssize = 2;
1434 stype = STT_OBJECT;
1435 break;
1436 case TY_DWORD:
1437 ssize = 4;
1438 stype = STT_OBJECT;
1439 break;
1440 case TY_FLOAT:
1441 ssize = 4;
1442 stype = STT_OBJECT;
1443 break;
1444 case TY_QWORD:
1445 ssize = 8;
1446 stype = STT_OBJECT;
1447 break;
1448 case TY_TBYTE:
1449 ssize = 10;
1450 stype = STT_OBJECT;
1451 break;
1452 case TY_OWORD:
1453 ssize = 16;
1454 stype = STT_OBJECT;
1455 break;
1456 case TY_YWORD:
1457 ssize = 32;
1458 stype = STT_OBJECT;
1459 break;
1460 case TY_COMMON:
1461 ssize = 0;
1462 stype = STT_COMMON;
1463 break;
1464 case TY_SEG:
1465 ssize = 0;
1466 stype = STT_SECTION;
1467 break;
1468 case TY_EXTERN:
1469 ssize = 0;
1470 stype = STT_NOTYPE;
1471 break;
1472 case TY_EQU:
1473 ssize = 0;
1474 stype = STT_NOTYPE;
1475 break;
1476 default:
1477 ssize = 0;
1478 stype = STT_NOTYPE;
1479 break;
1481 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1482 lastsym->size = ssize;
1483 lastsym->type = stype;
1487 /* stabs debugging routines */
1489 static void stabs_linenum(const char *filename, int32_t linenumber, int32_t segto)
1491 (void)segto;
1492 if (!stabs_filename) {
1493 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1494 strcpy(stabs_filename, filename);
1495 } else {
1496 if (strcmp(stabs_filename, filename)) {
1497 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1498 in fact, this leak comes in quite handy to maintain a list of files
1499 encountered so far in the symbol lines... */
1501 /* why not nasm_free(stabs_filename); we're done with the old one */
1503 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1504 strcpy(stabs_filename, filename);
1507 debug_immcall = 1;
1508 currentline = linenumber;
1511 static void stabs_output(int type, void *param)
1513 struct symlininfo *s;
1514 struct linelist *el;
1515 if (type == TY_DEBUGSYMLIN) {
1516 if (debug_immcall) {
1517 s = (struct symlininfo *)param;
1518 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1519 return; /* line info is only collected for executable sections */
1520 numlinestabs++;
1521 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1522 el->info.offset = s->offset;
1523 el->info.section = s->section;
1524 el->info.name = s->name;
1525 el->line = currentline;
1526 el->filename = stabs_filename;
1527 el->next = 0;
1528 if (stabslines) {
1529 stabslines->last->next = el;
1530 stabslines->last = el;
1531 } else {
1532 stabslines = el;
1533 stabslines->last = el;
1537 debug_immcall = 0;
1540 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1542 static void stabs_generate(void)
1544 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1545 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1546 char **allfiles;
1547 int *fileidx;
1549 struct linelist *ptr;
1551 ptr = stabslines;
1553 allfiles = (char **)nasm_zalloc(numlinestabs * sizeof(char *));
1554 numfiles = 0;
1555 while (ptr) {
1556 if (numfiles == 0) {
1557 allfiles[0] = ptr->filename;
1558 numfiles++;
1559 } else {
1560 for (i = 0; i < numfiles; i++) {
1561 if (!strcmp(allfiles[i], ptr->filename))
1562 break;
1564 if (i >= numfiles) {
1565 allfiles[i] = ptr->filename;
1566 numfiles++;
1569 ptr = ptr->next;
1571 strsize = 1;
1572 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1573 for (i = 0; i < numfiles; i++) {
1574 fileidx[i] = strsize;
1575 strsize += strlen(allfiles[i]) + 1;
1577 mainfileindex = 0;
1578 for (i = 0; i < numfiles; i++) {
1579 if (!strcmp(allfiles[i], elf_module)) {
1580 mainfileindex = i;
1581 break;
1586 * worst case size of the stab buffer would be:
1587 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1588 * plus one "ending" entry
1590 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1591 sizeof(struct stabentry));
1592 ssbuf = (uint8_t *)nasm_malloc(strsize);
1593 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1594 rptr = rbuf;
1596 for (i = 0; i < numfiles; i++)
1597 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1598 ssbuf[0] = 0;
1600 stabstrlen = strsize; /* set global variable for length of stab strings */
1602 sptr = sbuf;
1603 ptr = stabslines;
1604 numstabs = 0;
1606 if (ptr) {
1608 * this is the first stab, its strx points to the filename of the
1609 * the source-file, the n_desc field should be set to the number
1610 * of remaining stabs
1612 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, stabstrlen);
1614 /* this is the stab for the main source file */
1615 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1617 /* relocation table entry */
1620 * Since the symbol table has two entries before
1621 * the section symbols, the index in the info.section
1622 * member must be adjusted by adding 2
1625 WRITELONG(rptr, (sptr - sbuf) - 4);
1626 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
1628 numstabs++;
1629 currfile = mainfileindex;
1632 while (ptr) {
1633 if (strcmp(allfiles[currfile], ptr->filename)) {
1634 /* oops file has changed... */
1635 for (i = 0; i < numfiles; i++)
1636 if (!strcmp(allfiles[i], ptr->filename))
1637 break;
1638 currfile = i;
1639 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1640 ptr->info.offset);
1641 numstabs++;
1643 /* relocation table entry */
1645 WRITELONG(rptr, (sptr - sbuf) - 4);
1646 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
1649 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1650 numstabs++;
1652 /* relocation table entry */
1654 WRITELONG(rptr, (sptr - sbuf) - 4);
1655 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
1657 ptr = ptr->next;
1661 /* this is an "ending" token */
1662 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1663 numstabs++;
1665 ((struct stabentry *)sbuf)->n_desc = numstabs;
1667 nasm_free(allfiles);
1668 nasm_free(fileidx);
1670 stablen = (sptr - sbuf);
1671 stabrellen = (rptr - rbuf);
1672 stabrelbuf = rbuf;
1673 stabbuf = sbuf;
1674 stabstrbuf = ssbuf;
1677 static void stabs_cleanup(void)
1679 struct linelist *ptr, *del;
1680 if (!stabslines)
1681 return;
1683 ptr = stabslines;
1684 while (ptr) {
1685 del = ptr;
1686 ptr = ptr->next;
1687 nasm_free(del);
1690 nasm_free(stabbuf);
1691 nasm_free(stabrelbuf);
1692 nasm_free(stabstrbuf);
1695 /* dwarf routines */
1697 static void dwarf_init(void)
1699 ndebugs = 3; /* 3 debug symbols */
1702 static void dwarf_linenum(const char *filename, int32_t linenumber,
1703 int32_t segto)
1705 (void)segto;
1706 dwarf_findfile(filename);
1707 debug_immcall = 1;
1708 currentline = linenumber;
1711 /* called from elf_out with type == TY_DEBUGSYMLIN */
1712 static void dwarf_output(int type, void *param)
1714 int ln, aa, inx, maxln, soc;
1715 struct symlininfo *s;
1716 struct SAA *plinep;
1718 (void)type;
1720 s = (struct symlininfo *)param;
1722 /* line number info is only gathered for executable sections */
1723 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1724 return;
1726 /* Check if section index has changed */
1727 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1728 dwarf_findsect(s->section);
1730 /* do nothing unless line or file has changed */
1731 if (!debug_immcall)
1732 return;
1734 ln = currentline - dwarf_csect->line;
1735 aa = s->offset - dwarf_csect->offset;
1736 inx = dwarf_clist->line;
1737 plinep = dwarf_csect->psaa;
1738 /* check for file change */
1739 if (!(inx == dwarf_csect->file)) {
1740 saa_write8(plinep,DW_LNS_set_file);
1741 saa_write8(plinep,inx);
1742 dwarf_csect->file = inx;
1744 /* check for line change */
1745 if (ln) {
1746 /* test if in range of special op code */
1747 maxln = line_base + line_range;
1748 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1749 if (ln >= line_base && ln < maxln && soc < 256) {
1750 saa_write8(plinep,soc);
1751 } else {
1752 saa_write8(plinep,DW_LNS_advance_line);
1753 saa_wleb128s(plinep,ln);
1754 if (aa) {
1755 saa_write8(plinep,DW_LNS_advance_pc);
1756 saa_wleb128u(plinep,aa);
1759 dwarf_csect->line = currentline;
1760 dwarf_csect->offset = s->offset;
1763 /* show change handled */
1764 debug_immcall = 0;
1768 static void dwarf_generate(void)
1770 uint8_t *pbuf;
1771 int indx;
1772 struct linelist *ftentry;
1773 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1774 struct SAA *parangesrel, *plinesrel, *pinforel;
1775 struct sectlist *psect;
1776 size_t saalen, linepoff, totlen, highaddr;
1778 /* write epilogues for each line program range */
1779 /* and build aranges section */
1780 paranges = saa_init(1L);
1781 parangesrel = saa_init(1L);
1782 saa_write16(paranges,3); /* dwarf version */
1783 saa_write32(parangesrel, paranges->datalen+4);
1784 saa_write32(parangesrel, (dwarf_infosym << 8) + R_X86_64_32); /* reloc to info */
1785 saa_write32(parangesrel, 0);
1786 saa_write32(paranges,0); /* offset into info */
1787 saa_write8(paranges,4); /* pointer size */
1788 saa_write8(paranges,0); /* not segmented */
1789 saa_write32(paranges,0); /* padding */
1790 /* iterate though sectlist entries */
1791 psect = dwarf_fsect;
1792 totlen = 0;
1793 highaddr = 0;
1794 for (indx = 0; indx < dwarf_nsections; indx++)
1796 plinep = psect->psaa;
1797 /* Line Number Program Epilogue */
1798 saa_write8(plinep,2); /* std op 2 */
1799 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1800 saa_write8(plinep,DW_LNS_extended_op);
1801 saa_write8(plinep,1); /* operand length */
1802 saa_write8(plinep,DW_LNE_end_sequence);
1803 totlen += plinep->datalen;
1804 /* range table relocation entry */
1805 saa_write32(parangesrel, paranges->datalen + 4);
1806 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_X86_64_32);
1807 saa_write32(parangesrel, (uint32_t) 0);
1808 /* range table entry */
1809 saa_write32(paranges,0x0000); /* range start */
1810 saa_write32(paranges,sects[psect->section]->len); /* range length */
1811 highaddr += sects[psect->section]->len;
1812 /* done with this entry */
1813 psect = psect->next;
1815 saa_write32(paranges,0); /* null address */
1816 saa_write32(paranges,0); /* null length */
1817 saalen = paranges->datalen;
1818 arangeslen = saalen + 4;
1819 arangesbuf = pbuf = nasm_malloc(arangeslen);
1820 WRITELONG(pbuf,saalen); /* initial length */
1821 saa_rnbytes(paranges, pbuf, saalen);
1822 saa_free(paranges);
1824 /* build rela.aranges section */
1825 arangesrellen = saalen = parangesrel->datalen;
1826 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1827 saa_rnbytes(parangesrel, pbuf, saalen);
1828 saa_free(parangesrel);
1830 /* build pubnames section */
1831 ppubnames = saa_init(1L);
1832 saa_write16(ppubnames,3); /* dwarf version */
1833 saa_write32(ppubnames,0); /* offset into info */
1834 saa_write32(ppubnames,0); /* space used in info */
1835 saa_write32(ppubnames,0); /* end of list */
1836 saalen = ppubnames->datalen;
1837 pubnameslen = saalen + 4;
1838 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1839 WRITELONG(pbuf,saalen); /* initial length */
1840 saa_rnbytes(ppubnames, pbuf, saalen);
1841 saa_free(ppubnames);
1843 /* build info section */
1844 pinfo = saa_init(1L);
1845 pinforel = saa_init(1L);
1846 saa_write16(pinfo,3); /* dwarf version */
1847 saa_write32(pinforel, pinfo->datalen + 4);
1848 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_X86_64_32); /* reloc to abbrev */
1849 saa_write32(pinforel, 0);
1850 saa_write32(pinfo,0); /* offset into abbrev */
1851 saa_write8(pinfo,4); /* pointer size */
1852 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1853 saa_write32(pinforel, pinfo->datalen + 4);
1854 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
1855 saa_write32(pinforel, 0);
1856 saa_write32(pinfo,0); /* DW_AT_low_pc */
1857 saa_write32(pinforel, pinfo->datalen + 4);
1858 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
1859 saa_write32(pinforel, 0);
1860 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
1861 saa_write32(pinforel, pinfo->datalen + 4);
1862 saa_write32(pinforel, (dwarf_linesym << 8) + R_X86_64_32); /* reloc to line */
1863 saa_write32(pinforel, 0);
1864 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1865 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1866 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1867 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1868 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1869 saa_write32(pinforel, pinfo->datalen + 4);
1870 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
1871 saa_write32(pinforel, 0);
1872 saa_write32(pinfo,0); /* DW_AT_low_pc */
1873 saa_write32(pinfo,0); /* DW_AT_frame_base */
1874 saa_write8(pinfo,0); /* end of entries */
1875 saalen = pinfo->datalen;
1876 infolen = saalen + 4;
1877 infobuf = pbuf = nasm_malloc(infolen);
1878 WRITELONG(pbuf,saalen); /* initial length */
1879 saa_rnbytes(pinfo, pbuf, saalen);
1880 saa_free(pinfo);
1882 /* build rela.info section */
1883 inforellen = saalen = pinforel->datalen;
1884 inforelbuf = pbuf = nasm_malloc(inforellen);
1885 saa_rnbytes(pinforel, pbuf, saalen);
1886 saa_free(pinforel);
1888 /* build abbrev section */
1889 pabbrev = saa_init(1L);
1890 saa_write8(pabbrev,1); /* entry number LEB128u */
1891 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
1892 saa_write8(pabbrev,1); /* has children */
1893 /* the following attributes and forms are all LEB128u values */
1894 saa_write8(pabbrev,DW_AT_low_pc);
1895 saa_write8(pabbrev,DW_FORM_addr);
1896 saa_write8(pabbrev,DW_AT_high_pc);
1897 saa_write8(pabbrev,DW_FORM_addr);
1898 saa_write8(pabbrev,DW_AT_stmt_list);
1899 saa_write8(pabbrev,DW_FORM_data4);
1900 saa_write8(pabbrev,DW_AT_name);
1901 saa_write8(pabbrev,DW_FORM_string);
1902 saa_write8(pabbrev,DW_AT_producer);
1903 saa_write8(pabbrev,DW_FORM_string);
1904 saa_write8(pabbrev,DW_AT_language);
1905 saa_write8(pabbrev,DW_FORM_data2);
1906 saa_write16(pabbrev,0); /* end of entry */
1907 /* LEB128u usage same as above */
1908 saa_write8(pabbrev,2); /* entry number */
1909 saa_write8(pabbrev,DW_TAG_subprogram);
1910 saa_write8(pabbrev,0); /* no children */
1911 saa_write8(pabbrev,DW_AT_low_pc);
1912 saa_write8(pabbrev,DW_FORM_addr);
1913 saa_write8(pabbrev,DW_AT_frame_base);
1914 saa_write8(pabbrev,DW_FORM_data4);
1915 saa_write16(pabbrev,0); /* end of entry */
1916 abbrevlen = saalen = pabbrev->datalen;
1917 abbrevbuf = pbuf = nasm_malloc(saalen);
1918 saa_rnbytes(pabbrev, pbuf, saalen);
1919 saa_free(pabbrev);
1921 /* build line section */
1922 /* prolog */
1923 plines = saa_init(1L);
1924 saa_write8(plines,1); /* Minimum Instruction Length */
1925 saa_write8(plines,1); /* Initial value of 'is_stmt' */
1926 saa_write8(plines,line_base); /* Line Base */
1927 saa_write8(plines,line_range); /* Line Range */
1928 saa_write8(plines,opcode_base); /* Opcode Base */
1929 /* standard opcode lengths (# of LEB128u operands) */
1930 saa_write8(plines,0); /* Std opcode 1 length */
1931 saa_write8(plines,1); /* Std opcode 2 length */
1932 saa_write8(plines,1); /* Std opcode 3 length */
1933 saa_write8(plines,1); /* Std opcode 4 length */
1934 saa_write8(plines,1); /* Std opcode 5 length */
1935 saa_write8(plines,0); /* Std opcode 6 length */
1936 saa_write8(plines,0); /* Std opcode 7 length */
1937 saa_write8(plines,0); /* Std opcode 8 length */
1938 saa_write8(plines,1); /* Std opcode 9 length */
1939 saa_write8(plines,0); /* Std opcode 10 length */
1940 saa_write8(plines,0); /* Std opcode 11 length */
1941 saa_write8(plines,1); /* Std opcode 12 length */
1942 /* Directory Table */
1943 saa_write8(plines,0); /* End of table */
1944 /* File Name Table */
1945 ftentry = dwarf_flist;
1946 for (indx = 0; indx < dwarf_numfiles; indx++) {
1947 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
1948 saa_write8(plines,0); /* directory LEB128u */
1949 saa_write8(plines,0); /* time LEB128u */
1950 saa_write8(plines,0); /* size LEB128u */
1951 ftentry = ftentry->next;
1953 saa_write8(plines,0); /* End of table */
1954 linepoff = plines->datalen;
1955 linelen = linepoff + totlen + 10;
1956 linebuf = pbuf = nasm_malloc(linelen);
1957 WRITELONG(pbuf,linelen-4); /* initial length */
1958 WRITESHORT(pbuf,3); /* dwarf version */
1959 WRITELONG(pbuf,linepoff); /* offset to line number program */
1960 /* write line header */
1961 saalen = linepoff;
1962 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
1963 pbuf += linepoff;
1964 saa_free(plines);
1965 /* concatonate line program ranges */
1966 linepoff += 13;
1967 plinesrel = saa_init(1L);
1968 psect = dwarf_fsect;
1969 for (indx = 0; indx < dwarf_nsections; indx++) {
1970 saa_write32(plinesrel, linepoff);
1971 saa_write32(plinesrel, ((psect->section + 2) << 8) + R_X86_64_32);
1972 saa_write32(plinesrel, 0);
1973 plinep = psect->psaa;
1974 saalen = plinep->datalen;
1975 saa_rnbytes(plinep, pbuf, saalen);
1976 pbuf += saalen;
1977 linepoff += saalen;
1978 saa_free(plinep);
1979 /* done with this entry */
1980 psect = psect->next;
1984 /* build rela.lines section */
1985 linerellen =saalen = plinesrel->datalen;
1986 linerelbuf = pbuf = nasm_malloc(linerellen);
1987 saa_rnbytes(plinesrel, pbuf, saalen);
1988 saa_free(plinesrel);
1990 /* build frame section */
1991 framelen = 4;
1992 framebuf = pbuf = nasm_malloc(framelen);
1993 WRITELONG(pbuf,framelen-4); /* initial length */
1995 /* build loc section */
1996 loclen = 16;
1997 locbuf = pbuf = nasm_malloc(loclen);
1998 WRITELONG(pbuf,0); /* null beginning offset */
1999 WRITELONG(pbuf,0); /* null ending offset */
2002 static void dwarf_cleanup(void)
2004 nasm_free(arangesbuf);
2005 nasm_free(arangesrelbuf);
2006 nasm_free(pubnamesbuf);
2007 nasm_free(infobuf);
2008 nasm_free(inforelbuf);
2009 nasm_free(abbrevbuf);
2010 nasm_free(linebuf);
2011 nasm_free(linerelbuf);
2012 nasm_free(framebuf);
2013 nasm_free(locbuf);
2016 static void dwarf_findfile(const char * fname)
2018 int finx;
2019 struct linelist *match;
2021 /* return if fname is current file name */
2022 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2023 return;
2025 /* search for match */
2026 match = 0;
2027 if (dwarf_flist) {
2028 match = dwarf_flist;
2029 for (finx = 0; finx < dwarf_numfiles; finx++) {
2030 if (!(strcmp(fname, match->filename))) {
2031 dwarf_clist = match;
2032 return;
2037 /* add file name to end of list */
2038 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2039 dwarf_numfiles++;
2040 dwarf_clist->line = dwarf_numfiles;
2041 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2042 strcpy(dwarf_clist->filename,fname);
2043 dwarf_clist->next = 0;
2044 if (!dwarf_flist) { /* if first entry */
2045 dwarf_flist = dwarf_elist = dwarf_clist;
2046 dwarf_clist->last = 0;
2047 } else { /* chain to previous entry */
2048 dwarf_elist->next = dwarf_clist;
2049 dwarf_elist = dwarf_clist;
2053 static void dwarf_findsect(const int index)
2055 int sinx;
2056 struct sectlist *match;
2057 struct SAA *plinep;
2059 /* return if index is current section index */
2060 if (dwarf_csect && (dwarf_csect->section == index))
2061 return;
2063 /* search for match */
2064 match = 0;
2065 if (dwarf_fsect) {
2066 match = dwarf_fsect;
2067 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2068 if (match->section == index) {
2069 dwarf_csect = match;
2070 return;
2072 match = match->next;
2076 /* add entry to end of list */
2077 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2078 dwarf_nsections++;
2079 dwarf_csect->psaa = plinep = saa_init(1L);
2080 dwarf_csect->line = 1;
2081 dwarf_csect->offset = 0;
2082 dwarf_csect->file = 1;
2083 dwarf_csect->section = index;
2084 dwarf_csect->next = 0;
2085 /* set relocatable address at start of line program */
2086 saa_write8(plinep,DW_LNS_extended_op);
2087 saa_write8(plinep,5); /* operand length */
2088 saa_write8(plinep,DW_LNE_set_address);
2089 saa_write32(plinep,0); /* Start Address */
2091 if (!dwarf_fsect) { /* if first entry */
2092 dwarf_fsect = dwarf_esect = dwarf_csect;
2093 dwarf_csect->last = 0;
2094 } else { /* chain to previous entry */
2095 dwarf_esect->next = dwarf_csect;
2096 dwarf_esect = dwarf_csect;
2100 #endif /* OF_ELFX32 */