codeview.c: register all filenames
[nasm.git] / output / outelfx32.c
blob5af5cfb0f2587d2393e2f83d19c3162a178b6f72
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>
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);
164 /* stabs debugging routines */
165 static void stabsx32_linenum(const char *filename, int32_t linenumber, int32_t);
166 static void stabsx32_output(int, void *);
167 static void stabsx32_generate(void);
168 static void stabsx32_cleanup(void);
170 /* dwarf debugging routines */
171 static void dwarfx32_init(void);
172 static void dwarfx32_linenum(const char *filename, int32_t linenumber, int32_t);
173 static void dwarfx32_output(int, void *);
174 static void dwarfx32_generate(void);
175 static void dwarfx32_cleanup(void);
176 static void dwarfx32_findfile(const char *);
177 static void dwarfx32_findsect(const int);
180 * Special section numbers which are used to define ELF special
181 * symbols, which can be used with WRT to provide PIC relocation
182 * types.
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;
187 static int32_t elf_gottpoff_sect;
189 static void elf_init(void)
191 sects = NULL;
192 nsects = sectlen = 0;
193 syms = saa_init((int32_t)sizeof(struct elf_symbol));
194 nlocals = nglobs = ndebugs = 0;
195 bsym = raa_init();
196 strs = saa_init(1L);
197 saa_wbytes(strs, "\0", 1L);
198 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
199 strslen = 2 + strlen(elf_module);
200 shstrtab = NULL;
201 shstrtablen = shstrtabsize = 0;;
202 add_sectname("", "");
204 fwds = NULL;
206 elf_gotpc_sect = seg_alloc();
207 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
208 elf_gotoff_sect = seg_alloc();
209 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
210 elf_got_sect = seg_alloc();
211 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
212 elf_plt_sect = seg_alloc();
213 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
214 elf_sym_sect = seg_alloc();
215 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
216 elf_gottpoff_sect = seg_alloc();
217 define_label("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false);
219 def_seg = seg_alloc();
223 static void elf_cleanup(void)
225 struct elf_reloc *r;
226 int i;
228 elf_write();
229 for (i = 0; i < nsects; i++) {
230 if (sects[i]->type != SHT_NOBITS)
231 saa_free(sects[i]->data);
232 if (sects[i]->head)
233 saa_free(sects[i]->rel);
234 while (sects[i]->head) {
235 r = sects[i]->head;
236 sects[i]->head = sects[i]->head->next;
237 nasm_free(r);
240 nasm_free(sects);
241 saa_free(syms);
242 raa_free(bsym);
243 saa_free(strs);
244 dfmt->cleanup();
247 /* add entry to the elf .shstrtab section */
248 static void add_sectname(char *firsthalf, char *secondhalf)
250 int len = strlen(firsthalf) + strlen(secondhalf);
251 while (shstrtablen + len + 1 > shstrtabsize)
252 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
253 strcpy(shstrtab + shstrtablen, firsthalf);
254 strcat(shstrtab + shstrtablen, secondhalf);
255 shstrtablen += len + 1;
258 static int elf_make_section(char *name, int type, int flags, int align)
260 struct elf_section *s;
262 s = nasm_zalloc(sizeof(*s));
264 if (type != SHT_NOBITS)
265 s->data = saa_init(1L);
266 s->tail = &s->head;
267 if (!strcmp(name, ".text"))
268 s->index = def_seg;
269 else
270 s->index = seg_alloc();
271 add_sectname("", name);
273 s->name = nasm_strdup(name);
274 s->type = type;
275 s->flags = flags;
276 s->align = align;
278 if (nsects >= sectlen)
279 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
280 sects[nsects++] = s;
282 return nsects - 1;
285 static int32_t elf_section_names(char *name, int pass, int *bits)
287 char *p;
288 uint32_t flags, flags_and, flags_or;
289 uint64_t align;
290 int type, i;
293 * Default is 64 bits.
295 if (!name) {
296 *bits = 64;
297 return def_seg;
300 p = nasm_skip_word(name);
301 if (*p)
302 *p++ = '\0';
303 flags_and = flags_or = type = align = 0;
305 elf_section_attrib(name, p, pass, &flags_and,
306 &flags_or, &align, &type);
308 if (!strcmp(name, ".shstrtab") ||
309 !strcmp(name, ".symtab") ||
310 !strcmp(name, ".strtab")) {
311 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
312 "name `%s'", name);
313 return NO_SEG;
316 for (i = 0; i < nsects; i++)
317 if (!strcmp(name, sects[i]->name))
318 break;
319 if (i == nsects) {
320 const struct elf_known_section *ks = elf_known_sections;
322 while (ks->name) {
323 if (!strcmp(name, ks->name))
324 break;
325 ks++;
328 type = type ? type : ks->type;
329 align = align ? align : ks->align;
330 flags = (ks->flags & ~flags_and) | flags_or;
332 i = elf_make_section(name, type, flags, align);
333 } else if (pass == 1) {
334 if ((type && sects[i]->type != type)
335 || (align && sects[i]->align != align)
336 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
337 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
338 " redeclaration of section `%s'", name);
341 return sects[i]->index;
344 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
345 int is_global, char *special)
347 int pos = strslen;
348 struct elf_symbol *sym;
349 bool special_used = false;
351 #if defined(DEBUG) && DEBUG>2
352 nasm_error(ERR_DEBUG,
353 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
354 name, segment, offset, is_global, special);
355 #endif
356 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
358 * This is a NASM special symbol. We never allow it into
359 * the ELF symbol table, even if it's a valid one. If it
360 * _isn't_ a valid one, we should barf immediately.
362 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
363 strcmp(name, "..got") && strcmp(name, "..plt") &&
364 strcmp(name, "..sym") && strcmp(name, "..gottpoff"))
365 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
366 return;
369 if (is_global == 3) {
370 struct elf_symbol **s;
372 * Fix up a forward-reference symbol size from the first
373 * pass.
375 for (s = &fwds; *s; s = &(*s)->nextfwd)
376 if (!strcmp((*s)->name, name)) {
377 struct tokenval tokval;
378 expr *e;
379 char *p = nasm_skip_spaces(nasm_skip_word(special));
381 stdscan_reset();
382 stdscan_set(p);
383 tokval.t_type = TOKEN_INVALID;
384 e = evaluate(stdscan, NULL, &tokval, NULL, 1, NULL);
385 if (e) {
386 if (!is_simple(e))
387 nasm_error(ERR_NONFATAL, "cannot use relocatable"
388 " expression as symbol size");
389 else
390 (*s)->size = reloc_value(e);
394 * Remove it from the list of unresolved sizes.
396 nasm_free((*s)->name);
397 *s = (*s)->nextfwd;
398 return;
400 return; /* it wasn't an important one */
403 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
404 strslen += 1 + strlen(name);
406 lastsym = sym = saa_wstruct(syms);
408 memset(&sym->symv, 0, sizeof(struct rbtree));
410 sym->strpos = pos;
411 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
412 sym->other = STV_DEFAULT;
413 sym->size = 0;
414 if (segment == NO_SEG)
415 sym->section = SHN_ABS;
416 else {
417 int i;
418 sym->section = SHN_UNDEF;
419 if (segment == def_seg) {
420 /* we have to be sure at least text section is there */
421 int tempint;
422 if (segment != elf_section_names(".text", 2, &tempint))
423 nasm_panic(0, "strange segment conditions in ELF driver");
425 for (i = 0; i < nsects; i++) {
426 if (segment == sects[i]->index) {
427 sym->section = i + 1;
428 break;
433 if (is_global == 2) {
434 sym->size = offset;
435 sym->symv.key = 0;
436 sym->section = SHN_COMMON;
438 * We have a common variable. Check the special text to see
439 * if it's a valid number and power of two; if so, store it
440 * as the alignment for the common variable.
442 if (special) {
443 bool err;
444 sym->symv.key = readnum(special, &err);
445 if (err)
446 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
447 " valid number", special);
448 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
449 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
450 " power of two", special);
452 special_used = true;
453 } else
454 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
456 if (sym->type == SYM_GLOBAL) {
458 * If sym->section == SHN_ABS, then the first line of the
459 * else section would cause a core dump, because its a reference
460 * beyond the end of the section array.
461 * This behaviour is exhibited by this code:
462 * GLOBAL crash_nasm
463 * crash_nasm equ 0
464 * To avoid such a crash, such requests are silently discarded.
465 * This may not be the best solution.
467 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
468 bsym = raa_write(bsym, segment, nglobs);
469 } else if (sym->section != SHN_ABS) {
471 * This is a global symbol; so we must add it to the rbtree
472 * of global symbols in its section.
474 * In addition, we check the special text for symbol
475 * type and size information.
477 sects[sym->section-1]->gsyms =
478 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
480 if (special) {
481 int n = strcspn(special, " \t");
483 if (!nasm_strnicmp(special, "function", n))
484 sym->type |= STT_FUNC;
485 else if (!nasm_strnicmp(special, "data", n) ||
486 !nasm_strnicmp(special, "object", n))
487 sym->type |= STT_OBJECT;
488 else if (!nasm_strnicmp(special, "notype", n))
489 sym->type |= STT_NOTYPE;
490 else
491 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
492 n, special);
493 special += n;
495 special = nasm_skip_spaces(special);
496 if (*special) {
497 n = strcspn(special, " \t");
498 if (!nasm_strnicmp(special, "default", n))
499 sym->other = STV_DEFAULT;
500 else if (!nasm_strnicmp(special, "internal", n))
501 sym->other = STV_INTERNAL;
502 else if (!nasm_strnicmp(special, "hidden", n))
503 sym->other = STV_HIDDEN;
504 else if (!nasm_strnicmp(special, "protected", n))
505 sym->other = STV_PROTECTED;
506 else
507 n = 0;
508 special += n;
511 if (*special) {
512 struct tokenval tokval;
513 expr *e;
514 int fwd = 0;
515 char *saveme = stdscan_get();
517 while (special[n] && nasm_isspace(special[n]))
518 n++;
520 * We have a size expression; attempt to
521 * evaluate it.
523 stdscan_reset();
524 stdscan_set(special + n);
525 tokval.t_type = TOKEN_INVALID;
526 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, NULL);
527 if (fwd) {
528 sym->nextfwd = fwds;
529 fwds = sym;
530 sym->name = nasm_strdup(name);
531 } else if (e) {
532 if (!is_simple(e))
533 nasm_error(ERR_NONFATAL, "cannot use relocatable"
534 " expression as symbol size");
535 else
536 sym->size = reloc_value(e);
538 stdscan_set(saveme);
540 special_used = true;
543 * If TLS segment, mark symbol accordingly.
545 if (sects[sym->section - 1]->flags & SHF_TLS) {
546 sym->type &= 0xf0;
547 sym->type |= STT_TLS;
550 sym->globnum = nglobs;
551 nglobs++;
552 } else
553 nlocals++;
555 if (special && !special_used)
556 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
559 static void elf_add_reloc(struct elf_section *sect, int32_t segment,
560 int32_t offset, int type)
562 struct elf_reloc *r;
564 r = *sect->tail = nasm_zalloc(sizeof(struct elf_reloc));
565 sect->tail = &r->next;
567 r->address = sect->len;
568 r->offset = offset;
570 if (segment != NO_SEG) {
571 int i;
572 for (i = 0; i < nsects; i++)
573 if (segment == sects[i]->index)
574 r->symbol = i + 2;
575 if (!r->symbol)
576 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
578 r->type = type;
580 sect->nrelocs++;
584 * This routine deals with ..got and ..sym relocations: the more
585 * complicated kinds. In shared-library writing, some relocations
586 * with respect to global symbols must refer to the precise symbol
587 * rather than referring to an offset from the base of the section
588 * _containing_ the symbol. Such relocations call to this routine,
589 * which searches the symbol list for the symbol in question.
591 * R_X86_64_GOT32 references require the _exact_ symbol address to be
592 * used; R_X86_64_32 references can be at an offset from the symbol.
593 * The boolean argument `exact' tells us this.
595 * Return value is the adjusted value of `addr', having become an
596 * offset from the symbol rather than the section. Should always be
597 * zero when returning from an exact call.
599 * Limitation: if you define two symbols at the same place,
600 * confusion will occur.
602 * Inefficiency: we search, currently, using a linked list which
603 * isn't even necessarily sorted.
605 static void elf_add_gsym_reloc(struct elf_section *sect,
606 int32_t segment, uint32_t offset, int32_t pcrel,
607 int type, bool exact)
609 struct elf_reloc *r;
610 struct elf_section *s;
611 struct elf_symbol *sym;
612 struct rbtree *srb;
613 int i;
616 * First look up the segment/offset pair and find a global
617 * symbol corresponding to it. If it's not one of our segments,
618 * then it must be an external symbol, in which case we're fine
619 * doing a normal elf_add_reloc after first sanity-checking
620 * that the offset from the symbol is zero.
622 s = NULL;
623 for (i = 0; i < nsects; i++)
624 if (segment == sects[i]->index) {
625 s = sects[i];
626 break;
629 if (!s) {
630 if (exact && offset)
631 nasm_error(ERR_NONFATAL, "invalid access to an external symbol");
632 else
633 elf_add_reloc(sect, segment, offset - pcrel, type);
634 return;
637 srb = rb_search(s->gsyms, offset);
638 if (!srb || (exact && srb->key != offset)) {
639 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
640 " for this reference");
641 return;
643 sym = container_of(srb, struct elf_symbol, symv);
645 r = *sect->tail = nasm_malloc(sizeof(struct elf_reloc));
646 sect->tail = &r->next;
647 r->next = NULL;
649 r->address = sect->len;
650 r->offset = offset - pcrel - sym->symv.key;
651 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
652 r->type = type;
654 sect->nrelocs++;
657 static void elf_out(int32_t segto, const void *data,
658 enum out_type type, uint64_t size,
659 int32_t segment, int32_t wrt)
661 struct elf_section *s;
662 int32_t addr;
663 int reltype, bytes;
664 int i;
665 static struct symlininfo sinfo;
667 #if defined(DEBUG) && DEBUG>2
668 if (data)
669 nasm_error(ERR_DEBUG,
670 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64" data: %"PRIx64"\n",
671 currentline, type, segment, segto, size, *(int64_t *)data);
672 else
673 nasm_error(ERR_DEBUG,
674 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64"\n",
675 currentline, type, segment, segto, size);
676 #endif
679 * handle absolute-assembly (structure definitions)
681 if (segto == NO_SEG) {
682 if (type != OUT_RESERVE)
683 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
684 " space");
685 return;
688 s = NULL;
689 for (i = 0; i < nsects; i++)
690 if (segto == sects[i]->index) {
691 s = sects[i];
692 break;
694 if (!s) {
695 int tempint; /* ignored */
696 if (segto != elf_section_names(".text", 2, &tempint))
697 nasm_panic(0, "strange segment conditions in ELF driver");
698 else {
699 s = sects[nsects - 1];
700 i = nsects - 1;
704 /* again some stabs debugging stuff */
705 sinfo.offset = s->len;
706 sinfo.section = i;
707 sinfo.segto = segto;
708 sinfo.name = s->name;
709 dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
710 /* end of debugging stuff */
712 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
713 nasm_error(ERR_WARNING, "attempt to initialize memory in"
714 " BSS section `%s': ignored", s->name);
715 s->len += realsize(type, size);
716 return;
719 switch (type) {
720 case OUT_RESERVE:
721 if (s->type == SHT_PROGBITS) {
722 nasm_error(ERR_WARNING, "uninitialized space declared in"
723 " non-BSS section `%s': zeroing", s->name);
724 elf_sect_write(s, NULL, size);
725 } else
726 s->len += size;
727 break;
729 case OUT_RAWDATA:
730 if (segment != NO_SEG)
731 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
732 elf_sect_write(s, data, size);
733 break;
735 case OUT_ADDRESS:
737 int isize = (int)size;
738 int asize = abs((int)size);
740 addr = *(int64_t *)data;
741 if (segment == NO_SEG) {
742 /* Do nothing */
743 } else if (segment % 2) {
744 nasm_error(ERR_NONFATAL, "ELF format does not support"
745 " segment base references");
746 } else {
747 if (wrt == NO_SEG) {
748 switch (isize) {
749 case 1:
750 case -1:
751 elf_add_reloc(s, segment, addr, R_X86_64_8);
752 break;
753 case 2:
754 case -2:
755 elf_add_reloc(s, segment, addr, R_X86_64_16);
756 break;
757 case 4:
758 elf_add_reloc(s, segment, addr, R_X86_64_32);
759 break;
760 case -4:
761 elf_add_reloc(s, segment, addr, R_X86_64_32S);
762 break;
763 case 8:
764 case -8:
765 elf_add_reloc(s, segment, addr, R_X86_64_64);
766 break;
767 default:
768 nasm_panic(0, "internal error elfx32-hpa-871");
769 break;
771 addr = 0;
772 } else if (wrt == elf_gotpc_sect + 1) {
774 * The user will supply GOT relative to $$. ELF
775 * will let us have GOT relative to $. So we
776 * need to fix up the data item by $-$$.
778 addr += s->len;
779 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
780 addr = 0;
781 } else if (wrt == elf_gotoff_sect + 1) {
782 nasm_error(ERR_NONFATAL, "ELFX32 doesn't support "
783 "R_X86_64_GOTOFF64");
784 } else if (wrt == elf_got_sect + 1) {
785 switch (asize) {
786 case 4:
787 elf_add_gsym_reloc(s, segment, addr, 0,
788 R_X86_64_GOT32, true);
789 addr = 0;
790 break;
791 default:
792 nasm_error(ERR_NONFATAL, "invalid ..got reference");
793 break;
795 } else if (wrt == elf_sym_sect + 1) {
796 switch (isize) {
797 case 1:
798 case -1:
799 elf_add_gsym_reloc(s, segment, addr, 0,
800 R_X86_64_8, false);
801 addr = 0;
802 break;
803 case 2:
804 case -2:
805 elf_add_gsym_reloc(s, segment, addr, 0,
806 R_X86_64_16, false);
807 addr = 0;
808 break;
809 case 4:
810 elf_add_gsym_reloc(s, segment, addr, 0,
811 R_X86_64_32, false);
812 addr = 0;
813 break;
814 case -4:
815 elf_add_gsym_reloc(s, segment, addr, 0,
816 R_X86_64_32S, false);
817 addr = 0;
818 break;
819 case 8:
820 case -8:
821 elf_add_gsym_reloc(s, segment, addr, 0,
822 R_X86_64_64, false);
823 addr = 0;
824 break;
825 default:
826 nasm_panic(0, "internal error elfx32-hpa-903");
827 break;
829 } else if (wrt == elf_plt_sect + 1) {
830 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
831 "relative PLT references");
832 } else {
833 nasm_error(ERR_NONFATAL, "ELF format does not support this"
834 " use of WRT");
837 elf_sect_writeaddr(s, addr, asize);
838 break;
841 case OUT_REL1ADR:
842 reltype = R_X86_64_PC8;
843 bytes = 1;
844 goto rel12adr;
846 case OUT_REL2ADR:
847 reltype = R_X86_64_PC16;
848 bytes = 2;
849 goto rel12adr;
851 rel12adr:
852 addr = *(int64_t *)data - size;
853 if (segment == segto)
854 nasm_panic(0, "intra-segment OUT_REL1ADR");
855 if (segment == NO_SEG) {
856 /* Do nothing */
857 } else if (segment % 2) {
858 nasm_error(ERR_NONFATAL, "ELF format does not support"
859 " segment base references");
860 } else {
861 if (wrt == NO_SEG) {
862 elf_add_reloc(s, segment, addr, reltype);
863 addr = 0;
864 } else {
865 nasm_error(ERR_NONFATAL,
866 "Unsupported non-32-bit ELF relocation");
869 elf_sect_writeaddr(s, addr, bytes);
870 break;
872 case OUT_REL4ADR:
873 addr = *(int64_t *)data - size;
874 if (segment == segto)
875 nasm_panic(0, "intra-segment OUT_REL4ADR");
876 if (segment == NO_SEG) {
877 /* Do nothing */
878 } else if (segment % 2) {
879 nasm_error(ERR_NONFATAL, "ELFX32 format does not support"
880 " segment base references");
881 } else {
882 if (wrt == NO_SEG) {
883 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
884 addr = 0;
885 } else if (wrt == elf_plt_sect + 1) {
886 elf_add_gsym_reloc(s, segment, addr+size, size,
887 R_X86_64_PLT32, true);
888 addr = 0;
889 } else if (wrt == elf_gotpc_sect + 1 ||
890 wrt == elf_got_sect + 1) {
891 elf_add_gsym_reloc(s, segment, addr+size, size,
892 R_X86_64_GOTPCREL, true);
893 addr = 0;
894 } else if (wrt == elf_gotoff_sect + 1 ||
895 wrt == elf_got_sect + 1) {
896 nasm_error(ERR_NONFATAL, "invalid ..gotoff reference");
897 } else if (wrt == elf_gottpoff_sect + 1) {
898 elf_add_gsym_reloc(s, segment, addr+size, size,
899 R_X86_64_GOTTPOFF, true);
900 addr = 0;
901 } else {
902 nasm_error(ERR_NONFATAL, "ELFX32 format does not support this"
903 " use of WRT");
906 elf_sect_writeaddr(s, addr, 4);
907 break;
909 case OUT_REL8ADR:
910 nasm_error(ERR_NONFATAL,
911 "32-bit ELF format does not support 64-bit relocations");
912 addr = 0;
913 elf_sect_writeaddr(s, addr, 8);
914 break;
918 static void elf_write(void)
920 int align;
921 char *p;
922 int i;
924 struct SAA *symtab;
925 int32_t symtablen, symtablocal;
928 * Work out how many sections we will have. We have SHN_UNDEF,
929 * then the flexible user sections, then the fixed sections
930 * `.shstrtab', `.symtab' and `.strtab', then optionally
931 * relocation sections for the user sections.
933 nsections = sec_numspecial + 1;
934 if (dfmt == &df_stabs)
935 nsections += 3;
936 else if (dfmt == &df_dwarf)
937 nsections += 10;
939 add_sectname("", ".shstrtab");
940 add_sectname("", ".symtab");
941 add_sectname("", ".strtab");
942 for (i = 0; i < nsects; i++) {
943 nsections++; /* for the section itself */
944 if (sects[i]->head) {
945 nsections++; /* for its relocations */
946 add_sectname(".rela", sects[i]->name);
950 if (dfmt == &df_stabs) {
951 /* in case the debug information is wanted, just add these three sections... */
952 add_sectname("", ".stab");
953 add_sectname("", ".stabstr");
954 add_sectname(".rel", ".stab");
957 else if (dfmt == &df_dwarf) {
958 /* the dwarf debug standard specifies the following ten sections,
959 not all of which are currently implemented,
960 although all of them are defined. */
961 add_sectname("", ".debug_aranges");
962 add_sectname(".rela", ".debug_aranges");
963 add_sectname("", ".debug_pubnames");
964 add_sectname("", ".debug_info");
965 add_sectname(".rela", ".debug_info");
966 add_sectname("", ".debug_abbrev");
967 add_sectname("", ".debug_line");
968 add_sectname(".rela", ".debug_line");
969 add_sectname("", ".debug_frame");
970 add_sectname("", ".debug_loc");
974 * Output the ELF header.
976 nasm_write("\177ELF\1\1\1", 7, ofile);
977 fputc(elf_osabi, ofile);
978 fputc(elf_abiver, ofile);
979 fwritezero(7, ofile);
980 fwriteint16_t(ET_REL, ofile); /* relocatable file */
981 fwriteint16_t(EM_X86_64, ofile); /* processor ID */
982 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
983 fwriteint32_t(0L, ofile); /* no entry point */
984 fwriteint32_t(0L, ofile); /* no program header table */
985 fwriteint32_t(0x40L, ofile); /* section headers straight after
986 * ELF header plus alignment */
987 fwriteint32_t(0L, ofile); /* X86_64 defines no special flags */
988 fwriteint16_t(0x34, ofile); /* size of ELF header */
989 fwriteint16_t(0, ofile); /* no program header table, again */
990 fwriteint16_t(0, ofile); /* still no program header table */
991 fwriteint16_t(sizeof(Elf32_Shdr), ofile); /* size of section header */
992 fwriteint16_t(nsections, ofile); /* number of sections */
993 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
994 * section header table */
995 fwriteint32_t(0L, ofile); /* align to 0x40 bytes */
996 fwriteint32_t(0L, ofile);
997 fwriteint32_t(0L, ofile);
1000 * Build the symbol table and relocation tables.
1002 symtab = elf_build_symtab(&symtablen, &symtablocal);
1003 for (i = 0; i < nsects; i++)
1004 if (sects[i]->head)
1005 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1006 sects[i]->head);
1009 * Now output the section header table.
1012 elf_foffs = 0x40 + sizeof(Elf32_Shdr) * nsections;
1013 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1014 elf_foffs += align;
1015 elf_nsect = 0;
1016 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1018 /* SHN_UNDEF */
1019 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1020 p = shstrtab + 1;
1022 /* The normal sections */
1023 for (i = 0; i < nsects; i++) {
1024 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1025 (sects[i]->type == SHT_PROGBITS ?
1026 sects[i]->data : NULL), true,
1027 sects[i]->len, 0, 0, sects[i]->align, 0);
1028 p += strlen(p) + 1;
1031 /* .shstrtab */
1032 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1033 shstrtablen, 0, 0, 1, 0);
1034 p += strlen(p) + 1;
1036 /* .symtab */
1037 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1038 symtablen, sec_strtab, symtablocal, 4, 16);
1039 p += strlen(p) + 1;
1041 /* .strtab */
1042 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1043 strslen, 0, 0, 1, 0);
1044 p += strlen(p) + 1;
1046 /* The relocation sections */
1047 for (i = 0; i < nsects; i++)
1048 if (sects[i]->head) {
1049 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1050 sects[i]->rellen, sec_symtab, i + 1, 4, 12);
1051 p += strlen(p) + 1;
1054 if (dfmt == &df_stabs) {
1055 /* for debugging information, create the last three sections
1056 which are the .stab , .stabstr and .rel.stab sections respectively */
1058 /* this function call creates the stab sections in memory */
1059 stabsx32_generate();
1061 if (stabbuf && stabstrbuf && stabrelbuf) {
1062 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1063 stablen, sec_stabstr, 0, 4, 12);
1064 p += strlen(p) + 1;
1066 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1067 stabstrlen, 0, 0, 4, 0);
1068 p += strlen(p) + 1;
1070 /* link -> symtable info -> section to refer to */
1071 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1072 stabrellen, sec_symtab, sec_stab, 4, 8);
1073 p += strlen(p) + 1;
1075 } else if (dfmt == &df_dwarf) {
1076 /* for dwarf debugging information, create the ten dwarf sections */
1078 /* this function call creates the dwarf sections in memory */
1079 if (dwarf_fsect)
1080 dwarfx32_generate();
1082 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1083 arangeslen, 0, 0, 1, 0);
1084 p += strlen(p) + 1;
1086 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1087 arangesrellen, sec_symtab, sec_debug_aranges, 1, 12);
1088 p += strlen(p) + 1;
1090 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1091 pubnameslen, 0, 0, 1, 0);
1092 p += strlen(p) + 1;
1094 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1095 infolen, 0, 0, 1, 0);
1096 p += strlen(p) + 1;
1098 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1099 inforellen, sec_symtab, sec_debug_info, 1, 12);
1100 p += strlen(p) + 1;
1102 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1103 abbrevlen, 0, 0, 1, 0);
1104 p += strlen(p) + 1;
1106 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1107 linelen, 0, 0, 1, 0);
1108 p += strlen(p) + 1;
1110 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1111 linerellen, sec_symtab, sec_debug_line, 1, 12);
1112 p += strlen(p) + 1;
1114 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1115 framelen, 0, 0, 8, 0);
1116 p += strlen(p) + 1;
1118 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1119 loclen, 0, 0, 1, 0);
1120 p += strlen(p) + 1;
1122 fwritezero(align, ofile);
1125 * Now output the sections.
1127 elf_write_sections();
1129 nasm_free(elf_sects);
1130 saa_free(symtab);
1133 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1135 struct SAA *s = saa_init(1L);
1136 struct elf_symbol *sym;
1137 uint8_t entry[24], *p;
1138 int i;
1140 *len = *local = 0;
1143 * First, an all-zeros entry, required by the ELF spec.
1145 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1146 *len += 16;
1147 (*local)++;
1150 * Next, an entry for the file name.
1152 p = entry;
1153 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1154 WRITELONG(p, 0); /* no value */
1155 WRITELONG(p, 0); /* no size either */
1156 WRITESHORT(p, STT_FILE); /* type FILE */
1157 WRITESHORT(p, SHN_ABS);
1158 saa_wbytes(s, entry, 16L);
1159 *len += 16;
1160 (*local)++;
1163 * Now some standard symbols defining the segments, for relocation
1164 * purposes.
1166 for (i = 1; i <= nsects; i++) {
1167 p = entry;
1168 WRITELONG(p, 0); /* no symbol name */
1169 WRITELONG(p, 0); /* offset zero */
1170 WRITELONG(p, 0); /* size zero */
1171 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1172 WRITESHORT(p, i); /* section id */
1173 saa_wbytes(s, entry, 16L);
1174 *len += 16;
1175 (*local)++;
1180 * Now the other local symbols.
1182 saa_rewind(syms);
1183 while ((sym = saa_rstruct(syms))) {
1184 if (sym->type & SYM_GLOBAL)
1185 continue;
1186 p = entry;
1187 WRITELONG(p, sym->strpos); /* index into symbol string table */
1188 WRITELONG(p, sym->symv.key); /* value of symbol */
1189 WRITELONG(p, sym->size); /* size of symbol */
1190 WRITECHAR(p, sym->type); /* type and binding */
1191 WRITECHAR(p, sym->other); /* visibility */
1192 WRITESHORT(p, sym->section); /* index into section header table */
1193 saa_wbytes(s, entry, 16L);
1194 *len += 16;
1195 (*local)++;
1198 * dwarf needs symbols for debug sections
1199 * which are relocation targets.
1201 if (dfmt == &df_dwarf) {
1202 dwarf_infosym = *local;
1203 p = entry;
1204 WRITELONG(p, 0); /* no symbol name */
1205 WRITELONG(p, 0); /* offset zero */
1206 WRITELONG(p, 0); /* size zero */
1207 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1208 WRITESHORT(p, sec_debug_info); /* section id */
1209 saa_wbytes(s, entry, 16L);
1210 *len += 16;
1211 (*local)++;
1212 dwarf_abbrevsym = *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_abbrev); /* section id */
1219 saa_wbytes(s, entry, 16L);
1220 *len += 16;
1221 (*local)++;
1222 dwarf_linesym = *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_line); /* section id */
1229 saa_wbytes(s, entry, 16L);
1230 *len += 16;
1231 (*local)++;
1235 * Now the global symbols.
1237 saa_rewind(syms);
1238 while ((sym = saa_rstruct(syms))) {
1239 if (!(sym->type & SYM_GLOBAL))
1240 continue;
1241 p = entry;
1242 WRITELONG(p, sym->strpos);
1243 WRITELONG(p, sym->symv.key);
1244 WRITELONG(p, sym->size);
1245 WRITECHAR(p, sym->type); /* type and binding */
1246 WRITECHAR(p, sym->other); /* visibility */
1247 WRITESHORT(p, sym->section);
1248 saa_wbytes(s, entry, 16L);
1249 *len += 16;
1252 return s;
1255 static struct SAA *elf_build_reltab(uint64_t *len, struct elf_reloc *r)
1257 struct SAA *s;
1258 uint8_t *p, entry[12];
1259 int32_t global_offset;
1261 if (!r)
1262 return NULL;
1264 s = saa_init(1L);
1265 *len = 0;
1268 * How to onvert from a global placeholder to a real symbol index;
1269 * the +2 refers to the two special entries, the null entry and
1270 * the filename entry.
1272 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1274 while (r) {
1275 int32_t sym = r->symbol;
1277 if (sym >= GLOBAL_TEMP_BASE)
1278 sym += global_offset;
1280 p = entry;
1281 WRITELONG(p, r->address);
1282 WRITELONG(p, (sym << 8) + r->type);
1283 WRITELONG(p, r->offset);
1284 saa_wbytes(s, entry, 12L);
1285 *len += 12;
1287 r = r->next;
1290 return s;
1293 static void elf_section_header(int name, int type, uint32_t flags,
1294 void *data, bool is_saa, uint32_t datalen,
1295 int link, int info, int align, int eltsize)
1297 elf_sects[elf_nsect].data = data;
1298 elf_sects[elf_nsect].len = datalen;
1299 elf_sects[elf_nsect].is_saa = is_saa;
1300 elf_nsect++;
1302 fwriteint32_t((int32_t)name, ofile);
1303 fwriteint32_t((int32_t)type, ofile);
1304 fwriteint32_t((int32_t)flags, ofile);
1305 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
1306 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
1307 fwriteint32_t(datalen, ofile);
1308 if (data)
1309 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
1310 fwriteint32_t((int32_t)link, ofile);
1311 fwriteint32_t((int32_t)info, ofile);
1312 fwriteint32_t((int32_t)align, ofile);
1313 fwriteint32_t((int32_t)eltsize, ofile);
1316 static void elf_write_sections(void)
1318 int i;
1319 for (i = 0; i < elf_nsect; i++)
1320 if (elf_sects[i].data) {
1321 int32_t len = elf_sects[i].len;
1322 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
1323 int32_t align = reallen - len;
1324 if (elf_sects[i].is_saa)
1325 saa_fpwrite(elf_sects[i].data, ofile);
1326 else
1327 nasm_write(elf_sects[i].data, len, ofile);
1328 fwritezero(align, ofile);
1332 static void elf_sect_write(struct elf_section *sect, const void *data, size_t len)
1334 saa_wbytes(sect->data, data, len);
1335 sect->len += len;
1337 static void elf_sect_writeaddr(struct elf_section *sect, int32_t data, size_t len)
1339 saa_writeaddr(sect->data, data, len);
1340 sect->len += len;
1343 static void elf_sectalign(int32_t seg, unsigned int value)
1345 struct elf_section *s = NULL;
1346 int i;
1348 for (i = 0; i < nsects; i++) {
1349 if (sects[i]->index == seg) {
1350 s = sects[i];
1351 break;
1354 if (!s || !is_power2(value))
1355 return;
1357 if (value > s->align)
1358 s->align = value;
1361 static int32_t elf_segbase(int32_t segment)
1363 return segment;
1366 static void elf_filename(char *inname, char *outname)
1368 strcpy(elf_module, inname);
1369 standard_extension(inname, outname, ".o");
1372 extern macros_t elf_stdmac[];
1374 static int elf_set_info(enum geninfo type, char **val)
1376 (void)type;
1377 (void)val;
1378 return 0;
1380 static struct dfmt df_dwarf = {
1381 "ELFX32 (x86-64) dwarf debug format for Linux/Unix",
1382 "dwarf",
1383 dwarfx32_init,
1384 dwarfx32_linenum,
1385 null_debug_deflabel,
1386 null_debug_directive,
1387 debugx32_typevalue,
1388 dwarfx32_output,
1389 dwarfx32_cleanup
1391 static struct dfmt df_stabs = {
1392 "ELFX32 (x86-64) stabs debug format for Linux/Unix",
1393 "stabs",
1394 null_debug_init,
1395 stabsx32_linenum,
1396 null_debug_deflabel,
1397 null_debug_directive,
1398 debugx32_typevalue,
1399 stabsx32_output,
1400 stabsx32_cleanup
1403 struct dfmt *elfx32_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1405 struct ofmt of_elfx32 = {
1406 "ELFX32 (x86_64) object files (e.g. Linux)",
1407 "elfx32",
1410 elfx32_debugs_arr,
1411 &df_stabs,
1412 elf_stdmac,
1413 elf_init,
1414 elf_set_info,
1415 elf_out,
1416 elf_deflabel,
1417 elf_section_names,
1418 elf_sectalign,
1419 elf_segbase,
1420 elf_directive,
1421 elf_filename,
1422 elf_cleanup
1425 /* common debugging routines */
1426 static void debugx32_typevalue(int32_t type)
1428 int32_t stype, ssize;
1429 switch (TYM_TYPE(type)) {
1430 case TY_LABEL:
1431 ssize = 0;
1432 stype = STT_NOTYPE;
1433 break;
1434 case TY_BYTE:
1435 ssize = 1;
1436 stype = STT_OBJECT;
1437 break;
1438 case TY_WORD:
1439 ssize = 2;
1440 stype = STT_OBJECT;
1441 break;
1442 case TY_DWORD:
1443 ssize = 4;
1444 stype = STT_OBJECT;
1445 break;
1446 case TY_FLOAT:
1447 ssize = 4;
1448 stype = STT_OBJECT;
1449 break;
1450 case TY_QWORD:
1451 ssize = 8;
1452 stype = STT_OBJECT;
1453 break;
1454 case TY_TBYTE:
1455 ssize = 10;
1456 stype = STT_OBJECT;
1457 break;
1458 case TY_OWORD:
1459 ssize = 16;
1460 stype = STT_OBJECT;
1461 break;
1462 case TY_YWORD:
1463 ssize = 32;
1464 stype = STT_OBJECT;
1465 break;
1466 case TY_COMMON:
1467 ssize = 0;
1468 stype = STT_COMMON;
1469 break;
1470 case TY_SEG:
1471 ssize = 0;
1472 stype = STT_SECTION;
1473 break;
1474 case TY_EXTERN:
1475 ssize = 0;
1476 stype = STT_NOTYPE;
1477 break;
1478 case TY_EQU:
1479 ssize = 0;
1480 stype = STT_NOTYPE;
1481 break;
1482 default:
1483 ssize = 0;
1484 stype = STT_NOTYPE;
1485 break;
1487 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1488 lastsym->size = ssize;
1489 lastsym->type = stype;
1493 /* stabs debugging routines */
1495 static void stabsx32_linenum(const char *filename, int32_t linenumber, int32_t segto)
1497 (void)segto;
1498 if (!stabs_filename) {
1499 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1500 strcpy(stabs_filename, filename);
1501 } else {
1502 if (strcmp(stabs_filename, filename)) {
1503 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1504 in fact, this leak comes in quite handy to maintain a list of files
1505 encountered so far in the symbol lines... */
1507 /* why not nasm_free(stabs_filename); we're done with the old one */
1509 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1510 strcpy(stabs_filename, filename);
1513 debug_immcall = 1;
1514 currentline = linenumber;
1518 static void stabsx32_output(int type, void *param)
1520 struct symlininfo *s;
1521 struct linelist *el;
1522 if (type == TY_DEBUGSYMLIN) {
1523 if (debug_immcall) {
1524 s = (struct symlininfo *)param;
1525 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1526 return; /* line info is only collected for executable sections */
1527 numlinestabs++;
1528 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1529 el->info.offset = s->offset;
1530 el->info.section = s->section;
1531 el->info.name = s->name;
1532 el->line = currentline;
1533 el->filename = stabs_filename;
1534 el->next = 0;
1535 if (stabslines) {
1536 stabslines->last->next = el;
1537 stabslines->last = el;
1538 } else {
1539 stabslines = el;
1540 stabslines->last = el;
1544 debug_immcall = 0;
1547 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1549 static void stabsx32_generate(void)
1551 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1552 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1553 char **allfiles;
1554 int *fileidx;
1556 struct linelist *ptr;
1558 ptr = stabslines;
1560 allfiles = (char **)nasm_zalloc(numlinestabs * sizeof(char *));
1561 numfiles = 0;
1562 while (ptr) {
1563 if (numfiles == 0) {
1564 allfiles[0] = ptr->filename;
1565 numfiles++;
1566 } else {
1567 for (i = 0; i < numfiles; i++) {
1568 if (!strcmp(allfiles[i], ptr->filename))
1569 break;
1571 if (i >= numfiles) {
1572 allfiles[i] = ptr->filename;
1573 numfiles++;
1576 ptr = ptr->next;
1578 strsize = 1;
1579 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1580 for (i = 0; i < numfiles; i++) {
1581 fileidx[i] = strsize;
1582 strsize += strlen(allfiles[i]) + 1;
1584 mainfileindex = 0;
1585 for (i = 0; i < numfiles; i++) {
1586 if (!strcmp(allfiles[i], elf_module)) {
1587 mainfileindex = i;
1588 break;
1593 * worst case size of the stab buffer would be:
1594 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1595 * plus one "ending" entry
1597 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1598 sizeof(struct stabentry));
1599 ssbuf = (uint8_t *)nasm_malloc(strsize);
1600 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1601 rptr = rbuf;
1603 for (i = 0; i < numfiles; i++)
1604 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1605 ssbuf[0] = 0;
1607 stabstrlen = strsize; /* set global variable for length of stab strings */
1609 sptr = sbuf;
1610 ptr = stabslines;
1611 numstabs = 0;
1613 if (ptr) {
1615 * this is the first stab, its strx points to the filename of the
1616 * the source-file, the n_desc field should be set to the number
1617 * of remaining stabs
1619 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, stabstrlen);
1621 /* this is the stab for the main source file */
1622 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1624 /* relocation table entry */
1627 * Since the symbol table has two entries before
1628 * the section symbols, the index in the info.section
1629 * member must be adjusted by adding 2
1632 WRITELONG(rptr, (sptr - sbuf) - 4);
1633 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
1635 numstabs++;
1636 currfile = mainfileindex;
1639 while (ptr) {
1640 if (strcmp(allfiles[currfile], ptr->filename)) {
1641 /* oops file has changed... */
1642 for (i = 0; i < numfiles; i++)
1643 if (!strcmp(allfiles[i], ptr->filename))
1644 break;
1645 currfile = i;
1646 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1647 ptr->info.offset);
1648 numstabs++;
1650 /* relocation table entry */
1652 WRITELONG(rptr, (sptr - sbuf) - 4);
1653 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
1656 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1657 numstabs++;
1659 /* relocation table entry */
1661 WRITELONG(rptr, (sptr - sbuf) - 4);
1662 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
1664 ptr = ptr->next;
1668 /* this is an "ending" token */
1669 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1670 numstabs++;
1672 ((struct stabentry *)sbuf)->n_desc = numstabs;
1674 nasm_free(allfiles);
1675 nasm_free(fileidx);
1677 stablen = (sptr - sbuf);
1678 stabrellen = (rptr - rbuf);
1679 stabrelbuf = rbuf;
1680 stabbuf = sbuf;
1681 stabstrbuf = ssbuf;
1684 static void stabsx32_cleanup(void)
1686 struct linelist *ptr, *del;
1687 if (!stabslines)
1688 return;
1690 ptr = stabslines;
1691 while (ptr) {
1692 del = ptr;
1693 ptr = ptr->next;
1694 nasm_free(del);
1697 nasm_free(stabbuf);
1698 nasm_free(stabrelbuf);
1699 nasm_free(stabstrbuf);
1702 /* dwarf routines */
1704 static void dwarfx32_init(void)
1706 ndebugs = 3; /* 3 debug symbols */
1709 static void dwarfx32_linenum(const char *filename, int32_t linenumber,
1710 int32_t segto)
1712 (void)segto;
1713 dwarfx32_findfile(filename);
1714 debug_immcall = 1;
1715 currentline = linenumber;
1718 /* called from elf_out with type == TY_DEBUGSYMLIN */
1719 static void dwarfx32_output(int type, void *param)
1721 int ln, aa, inx, maxln, soc;
1722 struct symlininfo *s;
1723 struct SAA *plinep;
1725 (void)type;
1727 s = (struct symlininfo *)param;
1729 /* line number info is only gathered for executable sections */
1730 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1731 return;
1733 /* Check if section index has changed */
1734 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1735 dwarfx32_findsect(s->section);
1737 /* do nothing unless line or file has changed */
1738 if (!debug_immcall)
1739 return;
1741 ln = currentline - dwarf_csect->line;
1742 aa = s->offset - dwarf_csect->offset;
1743 inx = dwarf_clist->line;
1744 plinep = dwarf_csect->psaa;
1745 /* check for file change */
1746 if (!(inx == dwarf_csect->file)) {
1747 saa_write8(plinep,DW_LNS_set_file);
1748 saa_write8(plinep,inx);
1749 dwarf_csect->file = inx;
1751 /* check for line change */
1752 if (ln) {
1753 /* test if in range of special op code */
1754 maxln = line_base + line_range;
1755 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1756 if (ln >= line_base && ln < maxln && soc < 256) {
1757 saa_write8(plinep,soc);
1758 } else {
1759 saa_write8(plinep,DW_LNS_advance_line);
1760 saa_wleb128s(plinep,ln);
1761 if (aa) {
1762 saa_write8(plinep,DW_LNS_advance_pc);
1763 saa_wleb128u(plinep,aa);
1766 dwarf_csect->line = currentline;
1767 dwarf_csect->offset = s->offset;
1770 /* show change handled */
1771 debug_immcall = 0;
1775 static void dwarfx32_generate(void)
1777 uint8_t *pbuf;
1778 int indx;
1779 struct linelist *ftentry;
1780 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1781 struct SAA *parangesrel, *plinesrel, *pinforel;
1782 struct sectlist *psect;
1783 size_t saalen, linepoff, totlen, highaddr;
1785 /* write epilogues for each line program range */
1786 /* and build aranges section */
1787 paranges = saa_init(1L);
1788 parangesrel = saa_init(1L);
1789 saa_write16(paranges,3); /* dwarf version */
1790 saa_write32(parangesrel, paranges->datalen+4);
1791 saa_write32(parangesrel, (dwarf_infosym << 8) + R_X86_64_32); /* reloc to info */
1792 saa_write32(parangesrel, 0);
1793 saa_write32(paranges,0); /* offset into info */
1794 saa_write8(paranges,4); /* pointer size */
1795 saa_write8(paranges,0); /* not segmented */
1796 saa_write32(paranges,0); /* padding */
1797 /* iterate though sectlist entries */
1798 psect = dwarf_fsect;
1799 totlen = 0;
1800 highaddr = 0;
1801 for (indx = 0; indx < dwarf_nsections; indx++)
1803 plinep = psect->psaa;
1804 /* Line Number Program Epilogue */
1805 saa_write8(plinep,2); /* std op 2 */
1806 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1807 saa_write8(plinep,DW_LNS_extended_op);
1808 saa_write8(plinep,1); /* operand length */
1809 saa_write8(plinep,DW_LNE_end_sequence);
1810 totlen += plinep->datalen;
1811 /* range table relocation entry */
1812 saa_write32(parangesrel, paranges->datalen + 4);
1813 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_X86_64_32);
1814 saa_write32(parangesrel, (uint32_t) 0);
1815 /* range table entry */
1816 saa_write32(paranges,0x0000); /* range start */
1817 saa_write32(paranges,sects[psect->section]->len); /* range length */
1818 highaddr += sects[psect->section]->len;
1819 /* done with this entry */
1820 psect = psect->next;
1822 saa_write32(paranges,0); /* null address */
1823 saa_write32(paranges,0); /* null length */
1824 saalen = paranges->datalen;
1825 arangeslen = saalen + 4;
1826 arangesbuf = pbuf = nasm_malloc(arangeslen);
1827 WRITELONG(pbuf,saalen); /* initial length */
1828 saa_rnbytes(paranges, pbuf, saalen);
1829 saa_free(paranges);
1831 /* build rela.aranges section */
1832 arangesrellen = saalen = parangesrel->datalen;
1833 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1834 saa_rnbytes(parangesrel, pbuf, saalen);
1835 saa_free(parangesrel);
1837 /* build pubnames section */
1838 ppubnames = saa_init(1L);
1839 saa_write16(ppubnames,3); /* dwarf version */
1840 saa_write32(ppubnames,0); /* offset into info */
1841 saa_write32(ppubnames,0); /* space used in info */
1842 saa_write32(ppubnames,0); /* end of list */
1843 saalen = ppubnames->datalen;
1844 pubnameslen = saalen + 4;
1845 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1846 WRITELONG(pbuf,saalen); /* initial length */
1847 saa_rnbytes(ppubnames, pbuf, saalen);
1848 saa_free(ppubnames);
1850 /* build info section */
1851 pinfo = saa_init(1L);
1852 pinforel = saa_init(1L);
1853 saa_write16(pinfo,3); /* dwarf version */
1854 saa_write32(pinforel, pinfo->datalen + 4);
1855 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_X86_64_32); /* reloc to abbrev */
1856 saa_write32(pinforel, 0);
1857 saa_write32(pinfo,0); /* offset into abbrev */
1858 saa_write8(pinfo,4); /* pointer size */
1859 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1860 saa_write32(pinforel, pinfo->datalen + 4);
1861 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
1862 saa_write32(pinforel, 0);
1863 saa_write32(pinfo,0); /* DW_AT_low_pc */
1864 saa_write32(pinforel, pinfo->datalen + 4);
1865 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
1866 saa_write32(pinforel, 0);
1867 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
1868 saa_write32(pinforel, pinfo->datalen + 4);
1869 saa_write32(pinforel, (dwarf_linesym << 8) + R_X86_64_32); /* reloc to line */
1870 saa_write32(pinforel, 0);
1871 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1872 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1873 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1874 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1875 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1876 saa_write32(pinforel, pinfo->datalen + 4);
1877 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
1878 saa_write32(pinforel, 0);
1879 saa_write32(pinfo,0); /* DW_AT_low_pc */
1880 saa_write32(pinfo,0); /* DW_AT_frame_base */
1881 saa_write8(pinfo,0); /* end of entries */
1882 saalen = pinfo->datalen;
1883 infolen = saalen + 4;
1884 infobuf = pbuf = nasm_malloc(infolen);
1885 WRITELONG(pbuf,saalen); /* initial length */
1886 saa_rnbytes(pinfo, pbuf, saalen);
1887 saa_free(pinfo);
1889 /* build rela.info section */
1890 inforellen = saalen = pinforel->datalen;
1891 inforelbuf = pbuf = nasm_malloc(inforellen);
1892 saa_rnbytes(pinforel, pbuf, saalen);
1893 saa_free(pinforel);
1895 /* build abbrev section */
1896 pabbrev = saa_init(1L);
1897 saa_write8(pabbrev,1); /* entry number LEB128u */
1898 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
1899 saa_write8(pabbrev,1); /* has children */
1900 /* the following attributes and forms are all LEB128u values */
1901 saa_write8(pabbrev,DW_AT_low_pc);
1902 saa_write8(pabbrev,DW_FORM_addr);
1903 saa_write8(pabbrev,DW_AT_high_pc);
1904 saa_write8(pabbrev,DW_FORM_addr);
1905 saa_write8(pabbrev,DW_AT_stmt_list);
1906 saa_write8(pabbrev,DW_FORM_data4);
1907 saa_write8(pabbrev,DW_AT_name);
1908 saa_write8(pabbrev,DW_FORM_string);
1909 saa_write8(pabbrev,DW_AT_producer);
1910 saa_write8(pabbrev,DW_FORM_string);
1911 saa_write8(pabbrev,DW_AT_language);
1912 saa_write8(pabbrev,DW_FORM_data2);
1913 saa_write16(pabbrev,0); /* end of entry */
1914 /* LEB128u usage same as above */
1915 saa_write8(pabbrev,2); /* entry number */
1916 saa_write8(pabbrev,DW_TAG_subprogram);
1917 saa_write8(pabbrev,0); /* no children */
1918 saa_write8(pabbrev,DW_AT_low_pc);
1919 saa_write8(pabbrev,DW_FORM_addr);
1920 saa_write8(pabbrev,DW_AT_frame_base);
1921 saa_write8(pabbrev,DW_FORM_data4);
1922 saa_write16(pabbrev,0); /* end of entry */
1923 abbrevlen = saalen = pabbrev->datalen;
1924 abbrevbuf = pbuf = nasm_malloc(saalen);
1925 saa_rnbytes(pabbrev, pbuf, saalen);
1926 saa_free(pabbrev);
1928 /* build line section */
1929 /* prolog */
1930 plines = saa_init(1L);
1931 saa_write8(plines,1); /* Minimum Instruction Length */
1932 saa_write8(plines,1); /* Initial value of 'is_stmt' */
1933 saa_write8(plines,line_base); /* Line Base */
1934 saa_write8(plines,line_range); /* Line Range */
1935 saa_write8(plines,opcode_base); /* Opcode Base */
1936 /* standard opcode lengths (# of LEB128u operands) */
1937 saa_write8(plines,0); /* Std opcode 1 length */
1938 saa_write8(plines,1); /* Std opcode 2 length */
1939 saa_write8(plines,1); /* Std opcode 3 length */
1940 saa_write8(plines,1); /* Std opcode 4 length */
1941 saa_write8(plines,1); /* Std opcode 5 length */
1942 saa_write8(plines,0); /* Std opcode 6 length */
1943 saa_write8(plines,0); /* Std opcode 7 length */
1944 saa_write8(plines,0); /* Std opcode 8 length */
1945 saa_write8(plines,1); /* Std opcode 9 length */
1946 saa_write8(plines,0); /* Std opcode 10 length */
1947 saa_write8(plines,0); /* Std opcode 11 length */
1948 saa_write8(plines,1); /* Std opcode 12 length */
1949 /* Directory Table */
1950 saa_write8(plines,0); /* End of table */
1951 /* File Name Table */
1952 ftentry = dwarf_flist;
1953 for (indx = 0;indx<dwarf_numfiles;indx++)
1955 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
1956 saa_write8(plines,0); /* directory LEB128u */
1957 saa_write8(plines,0); /* time LEB128u */
1958 saa_write8(plines,0); /* size LEB128u */
1959 ftentry = ftentry->next;
1961 saa_write8(plines,0); /* End of table */
1962 linepoff = plines->datalen;
1963 linelen = linepoff + totlen + 10;
1964 linebuf = pbuf = nasm_malloc(linelen);
1965 WRITELONG(pbuf,linelen-4); /* initial length */
1966 WRITESHORT(pbuf,3); /* dwarf version */
1967 WRITELONG(pbuf,linepoff); /* offset to line number program */
1968 /* write line header */
1969 saalen = linepoff;
1970 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
1971 pbuf += linepoff;
1972 saa_free(plines);
1973 /* concatonate line program ranges */
1974 linepoff += 13;
1975 plinesrel = saa_init(1L);
1976 psect = dwarf_fsect;
1977 for (indx = 0; indx < dwarf_nsections; indx++) {
1978 saa_write32(plinesrel, linepoff);
1979 saa_write32(plinesrel, ((psect->section + 2) << 8) + R_X86_64_32);
1980 saa_write32(plinesrel, 0);
1981 plinep = psect->psaa;
1982 saalen = plinep->datalen;
1983 saa_rnbytes(plinep, pbuf, saalen);
1984 pbuf += saalen;
1985 linepoff += saalen;
1986 saa_free(plinep);
1987 /* done with this entry */
1988 psect = psect->next;
1992 /* build rela.lines section */
1993 linerellen =saalen = plinesrel->datalen;
1994 linerelbuf = pbuf = nasm_malloc(linerellen);
1995 saa_rnbytes(plinesrel, pbuf, saalen);
1996 saa_free(plinesrel);
1998 /* build frame section */
1999 framelen = 4;
2000 framebuf = pbuf = nasm_malloc(framelen);
2001 WRITELONG(pbuf,framelen-4); /* initial length */
2003 /* build loc section */
2004 loclen = 16;
2005 locbuf = pbuf = nasm_malloc(loclen);
2006 WRITELONG(pbuf,0); /* null beginning offset */
2007 WRITELONG(pbuf,0); /* null ending offset */
2010 static void dwarfx32_cleanup(void)
2012 nasm_free(arangesbuf);
2013 nasm_free(arangesrelbuf);
2014 nasm_free(pubnamesbuf);
2015 nasm_free(infobuf);
2016 nasm_free(inforelbuf);
2017 nasm_free(abbrevbuf);
2018 nasm_free(linebuf);
2019 nasm_free(linerelbuf);
2020 nasm_free(framebuf);
2021 nasm_free(locbuf);
2024 static void dwarfx32_findfile(const char * fname)
2026 int finx;
2027 struct linelist *match;
2029 /* return if fname is current file name */
2030 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2031 return;
2033 /* search for match */
2034 match = 0;
2035 if (dwarf_flist) {
2036 match = dwarf_flist;
2037 for (finx = 0; finx < dwarf_numfiles; finx++) {
2038 if (!(strcmp(fname, match->filename))) {
2039 dwarf_clist = match;
2040 return;
2045 /* add file name to end of list */
2046 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2047 dwarf_numfiles++;
2048 dwarf_clist->line = dwarf_numfiles;
2049 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2050 strcpy(dwarf_clist->filename,fname);
2051 dwarf_clist->next = 0;
2052 if (!dwarf_flist) { /* if first entry */
2053 dwarf_flist = dwarf_elist = dwarf_clist;
2054 dwarf_clist->last = 0;
2055 } else { /* chain to previous entry */
2056 dwarf_elist->next = dwarf_clist;
2057 dwarf_elist = dwarf_clist;
2061 static void dwarfx32_findsect(const int index)
2063 int sinx;
2064 struct sectlist *match;
2065 struct SAA *plinep;
2067 /* return if index is current section index */
2068 if (dwarf_csect && (dwarf_csect->section == index))
2069 return;
2071 /* search for match */
2072 match = 0;
2073 if (dwarf_fsect) {
2074 match = dwarf_fsect;
2075 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2076 if (match->section == index) {
2077 dwarf_csect = match;
2078 return;
2080 match = match->next;
2084 /* add entry to end of list */
2085 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2086 dwarf_nsections++;
2087 dwarf_csect->psaa = plinep = saa_init(1L);
2088 dwarf_csect->line = 1;
2089 dwarf_csect->offset = 0;
2090 dwarf_csect->file = 1;
2091 dwarf_csect->section = index;
2092 dwarf_csect->next = 0;
2093 /* set relocatable address at start of line program */
2094 saa_write8(plinep,DW_LNS_extended_op);
2095 saa_write8(plinep,5); /* operand length */
2096 saa_write8(plinep,DW_LNE_set_address);
2097 saa_write32(plinep,0); /* Start Address */
2099 if (!dwarf_fsect) { /* if first entry */
2100 dwarf_fsect = dwarf_esect = dwarf_csect;
2101 dwarf_csect->last = 0;
2102 } else { /* chain to previous entry */
2103 dwarf_esect->next = dwarf_csect;
2104 dwarf_esect = dwarf_csect;
2108 #endif /* OF_ELFX32 */