Merge remote-tracking branch 'origin/nasm-2.12.xx'
[nasm.git] / output / outelfx32.c
blob4dc795a93c1b8272f962f2787cb61d9ef27d8038
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2013 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * outelfx32.c output routines for the Netwide Assembler to produce
36 * ELF32 (x86_64) object file format
39 #include "compiler.h"
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <inttypes.h>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "saa.h"
50 #include "raa.h"
51 #include "stdscan.h"
52 #include "eval.h"
53 #include "output/outform.h"
54 #include "output/outlib.h"
55 #include "rbtree.h"
57 #include "output/dwarf.h"
58 #include "output/stabs.h"
59 #include "output/outelf.h"
61 #ifdef OF_ELFX32
63 #define SECT_DELTA 32
64 static struct elf_section **sects;
65 static int nsects, sectlen;
67 #define SHSTR_DELTA 256
68 static char *shstrtab;
69 static int shstrtablen, shstrtabsize;
71 static struct SAA *syms;
72 static uint32_t nlocals, nglobs, ndebugs; /* Symbol counts */
74 static int32_t def_seg;
76 static struct RAA *bsym;
78 static struct SAA *strs;
79 static uint32_t strslen;
81 static struct elf_symbol *fwds;
83 static char elf_module[FILENAME_MAX];
85 extern const 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 const struct dfmt df_dwarf;
158 static const struct dfmt df_stabs;
159 static struct elf_symbol *lastsym;
161 /* common debugging routines */
162 static void debug_typevalue(int32_t);
163 static void debug_deflabel(char *, int32_t, int64_t, int, char *);
164 static void debug_directive(const char *, const char *);
166 /* stabs debugging routines */
167 static void stabs_linenum(const char *filename, int32_t linenumber, int32_t);
168 static void stabs_output(int, void *);
169 static void stabs_generate(void);
170 static void stabs_cleanup(void);
172 /* dwarf debugging routines */
173 static void dwarf_init(void);
174 static void dwarf_linenum(const char *filename, int32_t linenumber, int32_t);
175 static void dwarf_output(int, void *);
176 static void dwarf_generate(void);
177 static void dwarf_cleanup(void);
178 static void dwarf_findfile(const char *);
179 static void dwarf_findsect(const int);
182 * Special section numbers which are used to define ELF special
183 * symbols, which can be used with WRT to provide PIC relocation
184 * types.
186 static int32_t elf_gotpc_sect, elf_gotoff_sect;
187 static int32_t elf_got_sect, elf_plt_sect;
188 static int32_t elf_sym_sect;
189 static int32_t elf_gottpoff_sect;
191 static void elf_init(void)
193 sects = NULL;
194 nsects = sectlen = 0;
195 syms = saa_init((int32_t)sizeof(struct elf_symbol));
196 nlocals = nglobs = ndebugs = 0;
197 bsym = raa_init();
198 strs = saa_init(1L);
199 saa_wbytes(strs, "\0", 1L);
200 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
201 strslen = 2 + strlen(elf_module);
202 shstrtab = NULL;
203 shstrtablen = shstrtabsize = 0;;
204 add_sectname("", "");
206 fwds = NULL;
208 elf_gotpc_sect = seg_alloc();
209 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
210 elf_gotoff_sect = seg_alloc();
211 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
212 elf_got_sect = seg_alloc();
213 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
214 elf_plt_sect = seg_alloc();
215 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
216 elf_sym_sect = seg_alloc();
217 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
218 elf_gottpoff_sect = seg_alloc();
219 define_label("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false);
221 def_seg = seg_alloc();
225 static void elf_cleanup(int debuginfo)
227 struct elf_reloc *r;
228 int i;
230 (void)debuginfo;
232 elf_write();
233 for (i = 0; i < nsects; i++) {
234 if (sects[i]->type != SHT_NOBITS)
235 saa_free(sects[i]->data);
236 if (sects[i]->head)
237 saa_free(sects[i]->rel);
238 while (sects[i]->head) {
239 r = sects[i]->head;
240 sects[i]->head = sects[i]->head->next;
241 nasm_free(r);
244 nasm_free(sects);
245 saa_free(syms);
246 raa_free(bsym);
247 saa_free(strs);
248 if (dfmt) {
249 dfmt->cleanup();
253 /* add entry to the elf .shstrtab section */
254 static void add_sectname(char *firsthalf, char *secondhalf)
256 int len = strlen(firsthalf) + strlen(secondhalf);
257 while (shstrtablen + len + 1 > shstrtabsize)
258 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
259 strcpy(shstrtab + shstrtablen, firsthalf);
260 strcat(shstrtab + shstrtablen, secondhalf);
261 shstrtablen += len + 1;
264 static int elf_make_section(char *name, int type, int flags, int align)
266 struct elf_section *s;
268 s = nasm_zalloc(sizeof(*s));
270 if (type != SHT_NOBITS)
271 s->data = saa_init(1L);
272 s->tail = &s->head;
273 if (!strcmp(name, ".text"))
274 s->index = def_seg;
275 else
276 s->index = seg_alloc();
277 add_sectname("", name);
279 s->name = nasm_strdup(name);
280 s->type = type;
281 s->flags = flags;
282 s->align = align;
284 if (nsects >= sectlen)
285 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
286 sects[nsects++] = s;
288 return nsects - 1;
291 static int32_t elf_section_names(char *name, int pass, int *bits)
293 char *p;
294 uint32_t flags, flags_and, flags_or;
295 uint64_t align;
296 int type, i;
299 * Default is 64 bits.
301 if (!name) {
302 *bits = 64;
303 return def_seg;
306 p = nasm_skip_word(name);
307 if (*p)
308 *p++ = '\0';
309 flags_and = flags_or = type = align = 0;
311 elf_section_attrib(name, p, pass, &flags_and,
312 &flags_or, &align, &type);
314 if (!strcmp(name, ".shstrtab") ||
315 !strcmp(name, ".symtab") ||
316 !strcmp(name, ".strtab")) {
317 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
318 "name `%s'", name);
319 return NO_SEG;
322 for (i = 0; i < nsects; i++)
323 if (!strcmp(name, sects[i]->name))
324 break;
325 if (i == nsects) {
326 const struct elf_known_section *ks = elf_known_sections;
328 while (ks->name) {
329 if (!strcmp(name, ks->name))
330 break;
331 ks++;
334 type = type ? type : ks->type;
335 align = align ? align : ks->align;
336 flags = (ks->flags & ~flags_and) | flags_or;
338 i = elf_make_section(name, type, flags, align);
339 } else if (pass == 1) {
340 if ((type && sects[i]->type != type)
341 || (align && sects[i]->align != align)
342 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
343 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
344 " redeclaration of section `%s'", name);
347 return sects[i]->index;
350 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
351 int is_global, char *special)
353 int pos = strslen;
354 struct elf_symbol *sym;
355 bool special_used = false;
357 #if defined(DEBUG) && DEBUG>2
358 nasm_error(ERR_DEBUG,
359 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
360 name, segment, offset, is_global, special);
361 #endif
362 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
364 * This is a NASM special symbol. We never allow it into
365 * the ELF symbol table, even if it's a valid one. If it
366 * _isn't_ a valid one, we should barf immediately.
368 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
369 strcmp(name, "..got") && strcmp(name, "..plt") &&
370 strcmp(name, "..sym") && strcmp(name, "..gottpoff"))
371 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
372 return;
375 if (is_global == 3) {
376 struct elf_symbol **s;
378 * Fix up a forward-reference symbol size from the first
379 * pass.
381 for (s = &fwds; *s; s = &(*s)->nextfwd)
382 if (!strcmp((*s)->name, name)) {
383 struct tokenval tokval;
384 expr *e;
385 char *p = nasm_skip_spaces(nasm_skip_word(special));
387 stdscan_reset();
388 stdscan_set(p);
389 tokval.t_type = TOKEN_INVALID;
390 e = evaluate(stdscan, NULL, &tokval, NULL, 1, NULL);
391 if (e) {
392 if (!is_simple(e))
393 nasm_error(ERR_NONFATAL, "cannot use relocatable"
394 " expression as symbol size");
395 else
396 (*s)->size = reloc_value(e);
400 * Remove it from the list of unresolved sizes.
402 nasm_free((*s)->name);
403 *s = (*s)->nextfwd;
404 return;
406 return; /* it wasn't an important one */
409 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
410 strslen += 1 + strlen(name);
412 lastsym = sym = saa_wstruct(syms);
414 memset(&sym->symv, 0, sizeof(struct rbtree));
416 sym->strpos = pos;
417 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
418 sym->other = STV_DEFAULT;
419 sym->size = 0;
420 if (segment == NO_SEG)
421 sym->section = SHN_ABS;
422 else {
423 int i;
424 sym->section = SHN_UNDEF;
425 if (segment == def_seg) {
426 /* we have to be sure at least text section is there */
427 int tempint;
428 if (segment != elf_section_names(".text", 2, &tempint))
429 nasm_panic(0, "strange segment conditions in ELF driver");
431 for (i = 0; i < nsects; i++) {
432 if (segment == sects[i]->index) {
433 sym->section = i + 1;
434 break;
439 if (is_global == 2) {
440 sym->size = offset;
441 sym->symv.key = 0;
442 sym->section = SHN_COMMON;
444 * We have a common variable. Check the special text to see
445 * if it's a valid number and power of two; if so, store it
446 * as the alignment for the common variable.
448 if (special) {
449 bool err;
450 sym->symv.key = readnum(special, &err);
451 if (err)
452 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
453 " valid number", special);
454 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
455 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
456 " power of two", special);
458 special_used = true;
459 } else
460 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
462 if (sym->type == SYM_GLOBAL) {
464 * If sym->section == SHN_ABS, then the first line of the
465 * else section would cause a core dump, because its a reference
466 * beyond the end of the section array.
467 * This behaviour is exhibited by this code:
468 * GLOBAL crash_nasm
469 * crash_nasm equ 0
470 * To avoid such a crash, such requests are silently discarded.
471 * This may not be the best solution.
473 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
474 bsym = raa_write(bsym, segment, nglobs);
475 } else if (sym->section != SHN_ABS) {
477 * This is a global symbol; so we must add it to the rbtree
478 * of global symbols in its section.
480 * In addition, we check the special text for symbol
481 * type and size information.
483 sects[sym->section-1]->gsyms =
484 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
486 if (special) {
487 int n = strcspn(special, " \t");
489 if (!nasm_strnicmp(special, "function", n))
490 sym->type |= STT_FUNC;
491 else if (!nasm_strnicmp(special, "data", n) ||
492 !nasm_strnicmp(special, "object", n))
493 sym->type |= STT_OBJECT;
494 else if (!nasm_strnicmp(special, "notype", n))
495 sym->type |= STT_NOTYPE;
496 else
497 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
498 n, special);
499 special += n;
501 special = nasm_skip_spaces(special);
502 if (*special) {
503 n = strcspn(special, " \t");
504 if (!nasm_strnicmp(special, "default", n))
505 sym->other = STV_DEFAULT;
506 else if (!nasm_strnicmp(special, "internal", n))
507 sym->other = STV_INTERNAL;
508 else if (!nasm_strnicmp(special, "hidden", n))
509 sym->other = STV_HIDDEN;
510 else if (!nasm_strnicmp(special, "protected", n))
511 sym->other = STV_PROTECTED;
512 else
513 n = 0;
514 special += n;
517 if (*special) {
518 struct tokenval tokval;
519 expr *e;
520 int fwd = 0;
521 char *saveme = stdscan_get();
523 while (special[n] && nasm_isspace(special[n]))
524 n++;
526 * We have a size expression; attempt to
527 * evaluate it.
529 stdscan_reset();
530 stdscan_set(special + n);
531 tokval.t_type = TOKEN_INVALID;
532 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, NULL);
533 if (fwd) {
534 sym->nextfwd = fwds;
535 fwds = sym;
536 sym->name = nasm_strdup(name);
537 } else if (e) {
538 if (!is_simple(e))
539 nasm_error(ERR_NONFATAL, "cannot use relocatable"
540 " expression as symbol size");
541 else
542 sym->size = reloc_value(e);
544 stdscan_set(saveme);
546 special_used = true;
549 * If TLS segment, mark symbol accordingly.
551 if (sects[sym->section - 1]->flags & SHF_TLS) {
552 sym->type &= 0xf0;
553 sym->type |= STT_TLS;
556 sym->globnum = nglobs;
557 nglobs++;
558 } else
559 nlocals++;
561 if (special && !special_used)
562 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
565 static void elf_add_reloc(struct elf_section *sect, int32_t segment,
566 int32_t offset, int type)
568 struct elf_reloc *r;
570 r = *sect->tail = nasm_zalloc(sizeof(struct elf_reloc));
571 sect->tail = &r->next;
573 r->address = sect->len;
574 r->offset = offset;
576 if (segment != NO_SEG) {
577 int i;
578 for (i = 0; i < nsects; i++)
579 if (segment == sects[i]->index)
580 r->symbol = i + 2;
581 if (!r->symbol)
582 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
584 r->type = type;
586 sect->nrelocs++;
590 * This routine deals with ..got and ..sym relocations: the more
591 * complicated kinds. In shared-library writing, some relocations
592 * with respect to global symbols must refer to the precise symbol
593 * rather than referring to an offset from the base of the section
594 * _containing_ the symbol. Such relocations call to this routine,
595 * which searches the symbol list for the symbol in question.
597 * R_X86_64_GOT32 references require the _exact_ symbol address to be
598 * used; R_X86_64_32 references can be at an offset from the symbol.
599 * The boolean argument `exact' tells us this.
601 * Return value is the adjusted value of `addr', having become an
602 * offset from the symbol rather than the section. Should always be
603 * zero when returning from an exact call.
605 * Limitation: if you define two symbols at the same place,
606 * confusion will occur.
608 * Inefficiency: we search, currently, using a linked list which
609 * isn't even necessarily sorted.
611 static void elf_add_gsym_reloc(struct elf_section *sect,
612 int32_t segment, uint32_t offset, int32_t pcrel,
613 int type, bool exact)
615 struct elf_reloc *r;
616 struct elf_section *s;
617 struct elf_symbol *sym;
618 struct rbtree *srb;
619 int i;
622 * First look up the segment/offset pair and find a global
623 * symbol corresponding to it. If it's not one of our segments,
624 * then it must be an external symbol, in which case we're fine
625 * doing a normal elf_add_reloc after first sanity-checking
626 * that the offset from the symbol is zero.
628 s = NULL;
629 for (i = 0; i < nsects; i++)
630 if (segment == sects[i]->index) {
631 s = sects[i];
632 break;
635 if (!s) {
636 if (exact && offset)
637 nasm_error(ERR_NONFATAL, "invalid access to an external symbol");
638 else
639 elf_add_reloc(sect, segment, offset - pcrel, type);
640 return;
643 srb = rb_search(s->gsyms, offset);
644 if (!srb || (exact && srb->key != offset)) {
645 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
646 " for this reference");
647 return;
649 sym = container_of(srb, struct elf_symbol, symv);
651 r = *sect->tail = nasm_malloc(sizeof(struct elf_reloc));
652 sect->tail = &r->next;
653 r->next = NULL;
655 r->address = sect->len;
656 r->offset = offset - pcrel - sym->symv.key;
657 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
658 r->type = type;
660 sect->nrelocs++;
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 int32_t addr;
669 int reltype, bytes;
670 int i;
671 static struct symlininfo sinfo;
673 #if defined(DEBUG) && DEBUG>2
674 if (data)
675 nasm_error(ERR_DEBUG,
676 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64" data: %"PRIx64"\n",
677 currentline, type, segment, segto, size, *(int64_t *)data);
678 else
679 nasm_error(ERR_DEBUG,
680 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64"\n",
681 currentline, type, segment, segto, size);
682 #endif
685 * handle absolute-assembly (structure definitions)
687 if (segto == NO_SEG) {
688 if (type != OUT_RESERVE)
689 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
690 " space");
691 return;
694 s = NULL;
695 for (i = 0; i < nsects; i++)
696 if (segto == sects[i]->index) {
697 s = sects[i];
698 break;
700 if (!s) {
701 int tempint; /* ignored */
702 if (segto != elf_section_names(".text", 2, &tempint))
703 nasm_panic(0, "strange segment conditions in ELF driver");
704 else {
705 s = sects[nsects - 1];
706 i = nsects - 1;
710 /* again some stabs debugging stuff */
711 if (dfmt) {
712 sinfo.offset = s->len;
713 sinfo.section = i;
714 sinfo.segto = segto;
715 sinfo.name = s->name;
716 dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
718 /* end of debugging stuff */
720 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
721 nasm_error(ERR_WARNING, "attempt to initialize memory in"
722 " BSS section `%s': ignored", s->name);
723 s->len += realsize(type, size);
724 return;
727 switch (type) {
728 case OUT_RESERVE:
729 if (s->type == SHT_PROGBITS) {
730 nasm_error(ERR_WARNING, "uninitialized space declared in"
731 " non-BSS section `%s': zeroing", s->name);
732 elf_sect_write(s, NULL, size);
733 } else
734 s->len += size;
735 break;
737 case OUT_RAWDATA:
738 if (segment != NO_SEG)
739 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
740 elf_sect_write(s, data, size);
741 break;
743 case OUT_ADDRESS:
745 int isize = (int)size;
746 int asize = abs((int)size);
748 addr = *(int64_t *)data;
749 if (segment == NO_SEG) {
750 /* Do nothing */
751 } else if (segment % 2) {
752 nasm_error(ERR_NONFATAL, "ELF format does not support"
753 " segment base references");
754 } else {
755 if (wrt == NO_SEG) {
756 switch (isize) {
757 case 1:
758 case -1:
759 elf_add_reloc(s, segment, addr, R_X86_64_8);
760 break;
761 case 2:
762 case -2:
763 elf_add_reloc(s, segment, addr, R_X86_64_16);
764 break;
765 case 4:
766 elf_add_reloc(s, segment, addr, R_X86_64_32);
767 break;
768 case -4:
769 elf_add_reloc(s, segment, addr, R_X86_64_32S);
770 break;
771 case 8:
772 case -8:
773 elf_add_reloc(s, segment, addr, R_X86_64_64);
774 break;
775 default:
776 nasm_panic(0, "internal error elfx32-hpa-871");
777 break;
779 addr = 0;
780 } else if (wrt == elf_gotpc_sect + 1) {
782 * The user will supply GOT relative to $$. ELF
783 * will let us have GOT relative to $. So we
784 * need to fix up the data item by $-$$.
786 addr += s->len;
787 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
788 addr = 0;
789 } else if (wrt == elf_gotoff_sect + 1) {
790 nasm_error(ERR_NONFATAL, "ELFX32 doesn't support "
791 "R_X86_64_GOTOFF64");
792 } else if (wrt == elf_got_sect + 1) {
793 switch (asize) {
794 case 4:
795 elf_add_gsym_reloc(s, segment, addr, 0,
796 R_X86_64_GOT32, true);
797 addr = 0;
798 break;
799 default:
800 nasm_error(ERR_NONFATAL, "invalid ..got reference");
801 break;
803 } else if (wrt == elf_sym_sect + 1) {
804 switch (isize) {
805 case 1:
806 case -1:
807 elf_add_gsym_reloc(s, segment, addr, 0,
808 R_X86_64_8, false);
809 addr = 0;
810 break;
811 case 2:
812 case -2:
813 elf_add_gsym_reloc(s, segment, addr, 0,
814 R_X86_64_16, false);
815 addr = 0;
816 break;
817 case 4:
818 elf_add_gsym_reloc(s, segment, addr, 0,
819 R_X86_64_32, false);
820 addr = 0;
821 break;
822 case -4:
823 elf_add_gsym_reloc(s, segment, addr, 0,
824 R_X86_64_32S, false);
825 addr = 0;
826 break;
827 case 8:
828 case -8:
829 elf_add_gsym_reloc(s, segment, addr, 0,
830 R_X86_64_64, false);
831 addr = 0;
832 break;
833 default:
834 nasm_panic(0, "internal error elfx32-hpa-903");
835 break;
837 } else if (wrt == elf_plt_sect + 1) {
838 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
839 "relative PLT references");
840 } else {
841 nasm_error(ERR_NONFATAL, "ELF format does not support this"
842 " use of WRT");
845 elf_sect_writeaddr(s, addr, asize);
846 break;
849 case OUT_REL1ADR:
850 reltype = R_X86_64_PC8;
851 bytes = 1;
852 goto rel12adr;
854 case OUT_REL2ADR:
855 reltype = R_X86_64_PC16;
856 bytes = 2;
857 goto rel12adr;
859 rel12adr:
860 addr = *(int64_t *)data - size;
861 if (segment == segto)
862 nasm_panic(0, "intra-segment OUT_REL1ADR");
863 if (segment == NO_SEG) {
864 /* Do nothing */
865 } else if (segment % 2) {
866 nasm_error(ERR_NONFATAL, "ELF format does not support"
867 " segment base references");
868 } else {
869 if (wrt == NO_SEG) {
870 elf_add_reloc(s, segment, addr, reltype);
871 addr = 0;
872 } else {
873 nasm_error(ERR_NONFATAL,
874 "Unsupported non-32-bit ELF relocation");
877 elf_sect_writeaddr(s, addr, bytes);
878 break;
880 case OUT_REL4ADR:
881 addr = *(int64_t *)data - size;
882 if (segment == segto)
883 nasm_panic(0, "intra-segment OUT_REL4ADR");
884 if (segment == NO_SEG) {
885 /* Do nothing */
886 } else if (segment % 2) {
887 nasm_error(ERR_NONFATAL, "ELFX32 format does not support"
888 " segment base references");
889 } else {
890 if (wrt == NO_SEG) {
891 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
892 addr = 0;
893 } else if (wrt == elf_plt_sect + 1) {
894 elf_add_gsym_reloc(s, segment, addr+size, size,
895 R_X86_64_PLT32, true);
896 addr = 0;
897 } else if (wrt == elf_gotpc_sect + 1 ||
898 wrt == elf_got_sect + 1) {
899 elf_add_gsym_reloc(s, segment, addr+size, size,
900 R_X86_64_GOTPCREL, true);
901 addr = 0;
902 } else if (wrt == elf_gotoff_sect + 1 ||
903 wrt == elf_got_sect + 1) {
904 nasm_error(ERR_NONFATAL, "invalid ..gotoff reference");
905 } else if (wrt == elf_gottpoff_sect + 1) {
906 elf_add_gsym_reloc(s, segment, addr+size, size,
907 R_X86_64_GOTTPOFF, true);
908 addr = 0;
909 } else {
910 nasm_error(ERR_NONFATAL, "ELFX32 format does not support this"
911 " use of WRT");
914 elf_sect_writeaddr(s, addr, 4);
915 break;
917 case OUT_REL8ADR:
918 nasm_error(ERR_NONFATAL,
919 "32-bit ELF format does not support 64-bit relocations");
920 addr = 0;
921 elf_sect_writeaddr(s, addr, 8);
922 break;
926 static void elf_write(void)
928 int align;
929 char *p;
930 int i;
932 struct SAA *symtab;
933 int32_t symtablen, symtablocal;
936 * Work out how many sections we will have. We have SHN_UNDEF,
937 * then the flexible user sections, then the fixed sections
938 * `.shstrtab', `.symtab' and `.strtab', then optionally
939 * relocation sections for the user sections.
941 nsections = sec_numspecial + 1;
942 if (dfmt == &df_stabs)
943 nsections += 3;
944 else if (dfmt == &df_dwarf)
945 nsections += 10;
947 add_sectname("", ".shstrtab");
948 add_sectname("", ".symtab");
949 add_sectname("", ".strtab");
950 for (i = 0; i < nsects; i++) {
951 nsections++; /* for the section itself */
952 if (sects[i]->head) {
953 nsections++; /* for its relocations */
954 add_sectname(".rela", sects[i]->name);
958 if (dfmt == &df_stabs) {
959 /* in case the debug information is wanted, just add these three sections... */
960 add_sectname("", ".stab");
961 add_sectname("", ".stabstr");
962 add_sectname(".rel", ".stab");
965 else if (dfmt == &df_dwarf) {
966 /* the dwarf debug standard specifies the following ten sections,
967 not all of which are currently implemented,
968 although all of them are defined. */
969 add_sectname("", ".debug_aranges");
970 add_sectname(".rela", ".debug_aranges");
971 add_sectname("", ".debug_pubnames");
972 add_sectname("", ".debug_info");
973 add_sectname(".rela", ".debug_info");
974 add_sectname("", ".debug_abbrev");
975 add_sectname("", ".debug_line");
976 add_sectname(".rela", ".debug_line");
977 add_sectname("", ".debug_frame");
978 add_sectname("", ".debug_loc");
982 * Output the ELF header.
984 nasm_write("\177ELF\1\1\1", 7, ofile);
985 fputc(elf_osabi, ofile);
986 fputc(elf_abiver, ofile);
987 fwritezero(7, ofile);
988 fwriteint16_t(ET_REL, ofile); /* relocatable file */
989 fwriteint16_t(EM_X86_64, ofile); /* processor ID */
990 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
991 fwriteint32_t(0L, ofile); /* no entry point */
992 fwriteint32_t(0L, ofile); /* no program header table */
993 fwriteint32_t(0x40L, ofile); /* section headers straight after
994 * ELF header plus alignment */
995 fwriteint32_t(0L, ofile); /* X86_64 defines no special flags */
996 fwriteint16_t(0x34, ofile); /* size of ELF header */
997 fwriteint16_t(0, ofile); /* no program header table, again */
998 fwriteint16_t(0, ofile); /* still no program header table */
999 fwriteint16_t(sizeof(Elf32_Shdr), ofile); /* size of section header */
1000 fwriteint16_t(nsections, ofile); /* number of sections */
1001 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
1002 * section header table */
1003 fwriteint32_t(0L, ofile); /* align to 0x40 bytes */
1004 fwriteint32_t(0L, ofile);
1005 fwriteint32_t(0L, ofile);
1008 * Build the symbol table and relocation tables.
1010 symtab = elf_build_symtab(&symtablen, &symtablocal);
1011 for (i = 0; i < nsects; i++)
1012 if (sects[i]->head)
1013 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1014 sects[i]->head);
1017 * Now output the section header table.
1020 elf_foffs = 0x40 + sizeof(Elf32_Shdr) * nsections;
1021 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1022 elf_foffs += align;
1023 elf_nsect = 0;
1024 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1026 /* SHN_UNDEF */
1027 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1028 p = shstrtab + 1;
1030 /* The normal sections */
1031 for (i = 0; i < nsects; i++) {
1032 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1033 (sects[i]->type == SHT_PROGBITS ?
1034 sects[i]->data : NULL), true,
1035 sects[i]->len, 0, 0, sects[i]->align, 0);
1036 p += strlen(p) + 1;
1039 /* .shstrtab */
1040 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1041 shstrtablen, 0, 0, 1, 0);
1042 p += strlen(p) + 1;
1044 /* .symtab */
1045 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1046 symtablen, sec_strtab, symtablocal, 4, 16);
1047 p += strlen(p) + 1;
1049 /* .strtab */
1050 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1051 strslen, 0, 0, 1, 0);
1052 p += strlen(p) + 1;
1054 /* The relocation sections */
1055 for (i = 0; i < nsects; i++)
1056 if (sects[i]->head) {
1057 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1058 sects[i]->rellen, sec_symtab, i + 1, 4, 12);
1059 p += strlen(p) + 1;
1062 if (dfmt == &df_stabs) {
1063 /* for debugging information, create the last three sections
1064 which are the .stab , .stabstr and .rel.stab sections respectively */
1066 /* this function call creates the stab sections in memory */
1067 stabs_generate();
1069 if (stabbuf && stabstrbuf && stabrelbuf) {
1070 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1071 stablen, sec_stabstr, 0, 4, 12);
1072 p += strlen(p) + 1;
1074 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1075 stabstrlen, 0, 0, 4, 0);
1076 p += strlen(p) + 1;
1078 /* link -> symtable info -> section to refer to */
1079 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1080 stabrellen, sec_symtab, sec_stab, 4, 8);
1081 p += strlen(p) + 1;
1083 } else if (dfmt == &df_dwarf) {
1084 /* for dwarf debugging information, create the ten dwarf sections */
1086 /* this function call creates the dwarf sections in memory */
1087 if (dwarf_fsect)
1088 dwarf_generate();
1090 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1091 arangeslen, 0, 0, 1, 0);
1092 p += strlen(p) + 1;
1094 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1095 arangesrellen, sec_symtab, sec_debug_aranges, 1, 12);
1096 p += strlen(p) + 1;
1098 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1099 pubnameslen, 0, 0, 1, 0);
1100 p += strlen(p) + 1;
1102 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1103 infolen, 0, 0, 1, 0);
1104 p += strlen(p) + 1;
1106 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1107 inforellen, sec_symtab, sec_debug_info, 1, 12);
1108 p += strlen(p) + 1;
1110 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1111 abbrevlen, 0, 0, 1, 0);
1112 p += strlen(p) + 1;
1114 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1115 linelen, 0, 0, 1, 0);
1116 p += strlen(p) + 1;
1118 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1119 linerellen, sec_symtab, sec_debug_line, 1, 12);
1120 p += strlen(p) + 1;
1122 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1123 framelen, 0, 0, 8, 0);
1124 p += strlen(p) + 1;
1126 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1127 loclen, 0, 0, 1, 0);
1128 p += strlen(p) + 1;
1130 fwritezero(align, ofile);
1133 * Now output the sections.
1135 elf_write_sections();
1137 nasm_free(elf_sects);
1138 saa_free(symtab);
1141 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1143 struct SAA *s = saa_init(1L);
1144 struct elf_symbol *sym;
1145 uint8_t entry[24], *p;
1146 int i;
1148 *len = *local = 0;
1151 * First, an all-zeros entry, required by the ELF spec.
1153 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1154 *len += 16;
1155 (*local)++;
1158 * Next, an entry for the file name.
1160 p = entry;
1161 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1162 WRITELONG(p, 0); /* no value */
1163 WRITELONG(p, 0); /* no size either */
1164 WRITESHORT(p, STT_FILE); /* type FILE */
1165 WRITESHORT(p, SHN_ABS);
1166 saa_wbytes(s, entry, 16L);
1167 *len += 16;
1168 (*local)++;
1171 * Now some standard symbols defining the segments, for relocation
1172 * purposes.
1174 for (i = 1; i <= nsects; i++) {
1175 p = entry;
1176 WRITELONG(p, 0); /* no symbol name */
1177 WRITELONG(p, 0); /* offset zero */
1178 WRITELONG(p, 0); /* size zero */
1179 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1180 WRITESHORT(p, i); /* section id */
1181 saa_wbytes(s, entry, 16L);
1182 *len += 16;
1183 (*local)++;
1188 * Now the other local symbols.
1190 saa_rewind(syms);
1191 while ((sym = saa_rstruct(syms))) {
1192 if (sym->type & SYM_GLOBAL)
1193 continue;
1194 p = entry;
1195 WRITELONG(p, sym->strpos); /* index into symbol string table */
1196 WRITELONG(p, sym->symv.key); /* value of symbol */
1197 WRITELONG(p, sym->size); /* size of symbol */
1198 WRITECHAR(p, sym->type); /* type and binding */
1199 WRITECHAR(p, sym->other); /* visibility */
1200 WRITESHORT(p, sym->section); /* index into section header table */
1201 saa_wbytes(s, entry, 16L);
1202 *len += 16;
1203 (*local)++;
1206 * dwarf needs symbols for debug sections
1207 * which are relocation targets.
1209 if (dfmt == &df_dwarf) {
1210 dwarf_infosym = *local;
1211 p = entry;
1212 WRITELONG(p, 0); /* no symbol name */
1213 WRITELONG(p, 0); /* offset zero */
1214 WRITELONG(p, 0); /* size zero */
1215 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1216 WRITESHORT(p, sec_debug_info); /* section id */
1217 saa_wbytes(s, entry, 16L);
1218 *len += 16;
1219 (*local)++;
1220 dwarf_abbrevsym = *local;
1221 p = entry;
1222 WRITELONG(p, 0); /* no symbol name */
1223 WRITELONG(p, 0); /* offset zero */
1224 WRITELONG(p, 0); /* size zero */
1225 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1226 WRITESHORT(p, sec_debug_abbrev); /* section id */
1227 saa_wbytes(s, entry, 16L);
1228 *len += 16;
1229 (*local)++;
1230 dwarf_linesym = *local;
1231 p = entry;
1232 WRITELONG(p, 0); /* no symbol name */
1233 WRITELONG(p, 0); /* offset zero */
1234 WRITELONG(p, 0); /* size zero */
1235 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1236 WRITESHORT(p, sec_debug_line); /* section id */
1237 saa_wbytes(s, entry, 16L);
1238 *len += 16;
1239 (*local)++;
1243 * Now the global symbols.
1245 saa_rewind(syms);
1246 while ((sym = saa_rstruct(syms))) {
1247 if (!(sym->type & SYM_GLOBAL))
1248 continue;
1249 p = entry;
1250 WRITELONG(p, sym->strpos);
1251 WRITELONG(p, sym->symv.key);
1252 WRITELONG(p, sym->size);
1253 WRITECHAR(p, sym->type); /* type and binding */
1254 WRITECHAR(p, sym->other); /* visibility */
1255 WRITESHORT(p, sym->section);
1256 saa_wbytes(s, entry, 16L);
1257 *len += 16;
1260 return s;
1263 static struct SAA *elf_build_reltab(uint64_t *len, struct elf_reloc *r)
1265 struct SAA *s;
1266 uint8_t *p, entry[12];
1267 int32_t global_offset;
1269 if (!r)
1270 return NULL;
1272 s = saa_init(1L);
1273 *len = 0;
1276 * How to onvert from a global placeholder to a real symbol index;
1277 * the +2 refers to the two special entries, the null entry and
1278 * the filename entry.
1280 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1282 while (r) {
1283 int32_t sym = r->symbol;
1285 if (sym >= GLOBAL_TEMP_BASE)
1286 sym += global_offset;
1288 p = entry;
1289 WRITELONG(p, r->address);
1290 WRITELONG(p, (sym << 8) + r->type);
1291 WRITELONG(p, r->offset);
1292 saa_wbytes(s, entry, 12L);
1293 *len += 12;
1295 r = r->next;
1298 return s;
1301 static void elf_section_header(int name, int type, uint32_t flags,
1302 void *data, bool is_saa, uint32_t datalen,
1303 int link, int info, int align, int eltsize)
1305 elf_sects[elf_nsect].data = data;
1306 elf_sects[elf_nsect].len = datalen;
1307 elf_sects[elf_nsect].is_saa = is_saa;
1308 elf_nsect++;
1310 fwriteint32_t((int32_t)name, ofile);
1311 fwriteint32_t((int32_t)type, ofile);
1312 fwriteint32_t((int32_t)flags, ofile);
1313 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
1314 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
1315 fwriteint32_t(datalen, ofile);
1316 if (data)
1317 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
1318 fwriteint32_t((int32_t)link, ofile);
1319 fwriteint32_t((int32_t)info, ofile);
1320 fwriteint32_t((int32_t)align, ofile);
1321 fwriteint32_t((int32_t)eltsize, ofile);
1324 static void elf_write_sections(void)
1326 int i;
1327 for (i = 0; i < elf_nsect; i++)
1328 if (elf_sects[i].data) {
1329 int32_t len = elf_sects[i].len;
1330 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
1331 int32_t align = reallen - len;
1332 if (elf_sects[i].is_saa)
1333 saa_fpwrite(elf_sects[i].data, ofile);
1334 else
1335 nasm_write(elf_sects[i].data, len, ofile);
1336 fwritezero(align, ofile);
1340 static void elf_sect_write(struct elf_section *sect, const void *data, size_t len)
1342 saa_wbytes(sect->data, data, len);
1343 sect->len += len;
1345 static void elf_sect_writeaddr(struct elf_section *sect, int32_t data, size_t len)
1347 saa_writeaddr(sect->data, data, len);
1348 sect->len += len;
1351 static void elf_sectalign(int32_t seg, unsigned int value)
1353 struct elf_section *s = NULL;
1354 int i;
1356 for (i = 0; i < nsects; i++) {
1357 if (sects[i]->index == seg) {
1358 s = sects[i];
1359 break;
1362 if (!s || !is_power2(value))
1363 return;
1365 if (value > s->align)
1366 s->align = value;
1369 static int32_t elf_segbase(int32_t segment)
1371 return segment;
1374 static void elf_filename(char *inname, char *outname)
1376 strcpy(elf_module, inname);
1377 standard_extension(inname, outname, ".o");
1380 extern macros_t elf_stdmac[];
1382 static int elf_set_info(enum geninfo type, char **val)
1384 (void)type;
1385 (void)val;
1386 return 0;
1388 static const struct dfmt df_dwarf = {
1389 "ELFX32 (x86-64) dwarf debug format for Linux/Unix",
1390 "dwarf",
1391 dwarf_init,
1392 dwarf_linenum,
1393 debug_deflabel,
1394 debug_directive,
1395 debug_typevalue,
1396 dwarf_output,
1397 dwarf_cleanup
1399 static const struct dfmt df_stabs = {
1400 "ELFX32 (x86-64) stabs debug format for Linux/Unix",
1401 "stabs",
1402 null_debug_init,
1403 stabs_linenum,
1404 debug_deflabel,
1405 debug_directive,
1406 debug_typevalue,
1407 stabs_output,
1408 stabs_cleanup
1411 static const struct dfmt * const elfx32_debugs_arr[3] =
1412 { &df_dwarf, &df_stabs, NULL };
1414 const struct ofmt of_elfx32 = {
1415 "ELFX32 (x86_64) object files (e.g. Linux)",
1416 "elfx32",
1419 elfx32_debugs_arr,
1420 &df_stabs,
1421 elf_stdmac,
1422 elf_init,
1423 elf_set_info,
1424 elf_out,
1425 elf_deflabel,
1426 elf_section_names,
1427 elf_sectalign,
1428 elf_segbase,
1429 elf_directive,
1430 elf_filename,
1431 elf_cleanup
1434 /* common debugging routines */
1435 static void debug_deflabel(char *name, int32_t segment, int64_t offset,
1436 int is_global, char *special)
1438 (void)name;
1439 (void)segment;
1440 (void)offset;
1441 (void)is_global;
1442 (void)special;
1445 static void debug_directive(const char *directive, const char *params)
1447 (void)directive;
1448 (void)params;
1451 static void debug_typevalue(int32_t type)
1453 int32_t stype, ssize;
1454 switch (TYM_TYPE(type)) {
1455 case TY_LABEL:
1456 ssize = 0;
1457 stype = STT_NOTYPE;
1458 break;
1459 case TY_BYTE:
1460 ssize = 1;
1461 stype = STT_OBJECT;
1462 break;
1463 case TY_WORD:
1464 ssize = 2;
1465 stype = STT_OBJECT;
1466 break;
1467 case TY_DWORD:
1468 ssize = 4;
1469 stype = STT_OBJECT;
1470 break;
1471 case TY_FLOAT:
1472 ssize = 4;
1473 stype = STT_OBJECT;
1474 break;
1475 case TY_QWORD:
1476 ssize = 8;
1477 stype = STT_OBJECT;
1478 break;
1479 case TY_TBYTE:
1480 ssize = 10;
1481 stype = STT_OBJECT;
1482 break;
1483 case TY_OWORD:
1484 ssize = 16;
1485 stype = STT_OBJECT;
1486 break;
1487 case TY_YWORD:
1488 ssize = 32;
1489 stype = STT_OBJECT;
1490 break;
1491 case TY_COMMON:
1492 ssize = 0;
1493 stype = STT_COMMON;
1494 break;
1495 case TY_SEG:
1496 ssize = 0;
1497 stype = STT_SECTION;
1498 break;
1499 case TY_EXTERN:
1500 ssize = 0;
1501 stype = STT_NOTYPE;
1502 break;
1503 case TY_EQU:
1504 ssize = 0;
1505 stype = STT_NOTYPE;
1506 break;
1507 default:
1508 ssize = 0;
1509 stype = STT_NOTYPE;
1510 break;
1512 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1513 lastsym->size = ssize;
1514 lastsym->type = stype;
1518 /* stabs debugging routines */
1520 static void stabs_linenum(const char *filename, int32_t linenumber, int32_t segto)
1522 (void)segto;
1523 if (!stabs_filename) {
1524 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1525 strcpy(stabs_filename, filename);
1526 } else {
1527 if (strcmp(stabs_filename, filename)) {
1528 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1529 in fact, this leak comes in quite handy to maintain a list of files
1530 encountered so far in the symbol lines... */
1532 /* why not nasm_free(stabs_filename); we're done with the old one */
1534 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1535 strcpy(stabs_filename, filename);
1538 debug_immcall = 1;
1539 currentline = linenumber;
1543 static void stabs_output(int type, void *param)
1545 struct symlininfo *s;
1546 struct linelist *el;
1547 if (type == TY_DEBUGSYMLIN) {
1548 if (debug_immcall) {
1549 s = (struct symlininfo *)param;
1550 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1551 return; /* line info is only collected for executable sections */
1552 numlinestabs++;
1553 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1554 el->info.offset = s->offset;
1555 el->info.section = s->section;
1556 el->info.name = s->name;
1557 el->line = currentline;
1558 el->filename = stabs_filename;
1559 el->next = 0;
1560 if (stabslines) {
1561 stabslines->last->next = el;
1562 stabslines->last = el;
1563 } else {
1564 stabslines = el;
1565 stabslines->last = el;
1569 debug_immcall = 0;
1572 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1574 static void stabs_generate(void)
1576 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1577 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1578 char **allfiles;
1579 int *fileidx;
1581 struct linelist *ptr;
1583 ptr = stabslines;
1585 allfiles = (char **)nasm_zalloc(numlinestabs * sizeof(char *));
1586 numfiles = 0;
1587 while (ptr) {
1588 if (numfiles == 0) {
1589 allfiles[0] = ptr->filename;
1590 numfiles++;
1591 } else {
1592 for (i = 0; i < numfiles; i++) {
1593 if (!strcmp(allfiles[i], ptr->filename))
1594 break;
1596 if (i >= numfiles) {
1597 allfiles[i] = ptr->filename;
1598 numfiles++;
1601 ptr = ptr->next;
1603 strsize = 1;
1604 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1605 for (i = 0; i < numfiles; i++) {
1606 fileidx[i] = strsize;
1607 strsize += strlen(allfiles[i]) + 1;
1609 mainfileindex = 0;
1610 for (i = 0; i < numfiles; i++) {
1611 if (!strcmp(allfiles[i], elf_module)) {
1612 mainfileindex = i;
1613 break;
1618 * worst case size of the stab buffer would be:
1619 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1620 * plus one "ending" entry
1622 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1623 sizeof(struct stabentry));
1624 ssbuf = (uint8_t *)nasm_malloc(strsize);
1625 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1626 rptr = rbuf;
1628 for (i = 0; i < numfiles; i++)
1629 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1630 ssbuf[0] = 0;
1632 stabstrlen = strsize; /* set global variable for length of stab strings */
1634 sptr = sbuf;
1635 ptr = stabslines;
1636 numstabs = 0;
1638 if (ptr) {
1640 * this is the first stab, its strx points to the filename of the
1641 * the source-file, the n_desc field should be set to the number
1642 * of remaining stabs
1644 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, stabstrlen);
1646 /* this is the stab for the main source file */
1647 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1649 /* relocation table entry */
1652 * Since the symbol table has two entries before
1653 * the section symbols, the index in the info.section
1654 * member must be adjusted by adding 2
1657 WRITELONG(rptr, (sptr - sbuf) - 4);
1658 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
1660 numstabs++;
1661 currfile = mainfileindex;
1664 while (ptr) {
1665 if (strcmp(allfiles[currfile], ptr->filename)) {
1666 /* oops file has changed... */
1667 for (i = 0; i < numfiles; i++)
1668 if (!strcmp(allfiles[i], ptr->filename))
1669 break;
1670 currfile = i;
1671 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1672 ptr->info.offset);
1673 numstabs++;
1675 /* relocation table entry */
1677 WRITELONG(rptr, (sptr - sbuf) - 4);
1678 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
1681 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1682 numstabs++;
1684 /* relocation table entry */
1686 WRITELONG(rptr, (sptr - sbuf) - 4);
1687 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
1689 ptr = ptr->next;
1693 /* this is an "ending" token */
1694 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1695 numstabs++;
1697 ((struct stabentry *)sbuf)->n_desc = numstabs;
1699 nasm_free(allfiles);
1700 nasm_free(fileidx);
1702 stablen = (sptr - sbuf);
1703 stabrellen = (rptr - rbuf);
1704 stabrelbuf = rbuf;
1705 stabbuf = sbuf;
1706 stabstrbuf = ssbuf;
1709 static void stabs_cleanup(void)
1711 struct linelist *ptr, *del;
1712 if (!stabslines)
1713 return;
1715 ptr = stabslines;
1716 while (ptr) {
1717 del = ptr;
1718 ptr = ptr->next;
1719 nasm_free(del);
1722 nasm_free(stabbuf);
1723 nasm_free(stabrelbuf);
1724 nasm_free(stabstrbuf);
1727 /* dwarf routines */
1729 static void dwarf_init(void)
1731 ndebugs = 3; /* 3 debug symbols */
1734 static void dwarf_linenum(const char *filename, int32_t linenumber,
1735 int32_t segto)
1737 (void)segto;
1738 dwarf_findfile(filename);
1739 debug_immcall = 1;
1740 currentline = linenumber;
1743 /* called from elf_out with type == TY_DEBUGSYMLIN */
1744 static void dwarf_output(int type, void *param)
1746 int ln, aa, inx, maxln, soc;
1747 struct symlininfo *s;
1748 struct SAA *plinep;
1750 (void)type;
1752 s = (struct symlininfo *)param;
1754 /* line number info is only gathered for executable sections */
1755 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1756 return;
1758 /* Check if section index has changed */
1759 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1760 dwarf_findsect(s->section);
1762 /* do nothing unless line or file has changed */
1763 if (!debug_immcall)
1764 return;
1766 ln = currentline - dwarf_csect->line;
1767 aa = s->offset - dwarf_csect->offset;
1768 inx = dwarf_clist->line;
1769 plinep = dwarf_csect->psaa;
1770 /* check for file change */
1771 if (!(inx == dwarf_csect->file)) {
1772 saa_write8(plinep,DW_LNS_set_file);
1773 saa_write8(plinep,inx);
1774 dwarf_csect->file = inx;
1776 /* check for line change */
1777 if (ln) {
1778 /* test if in range of special op code */
1779 maxln = line_base + line_range;
1780 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1781 if (ln >= line_base && ln < maxln && soc < 256) {
1782 saa_write8(plinep,soc);
1783 } else {
1784 saa_write8(plinep,DW_LNS_advance_line);
1785 saa_wleb128s(plinep,ln);
1786 if (aa) {
1787 saa_write8(plinep,DW_LNS_advance_pc);
1788 saa_wleb128u(plinep,aa);
1791 dwarf_csect->line = currentline;
1792 dwarf_csect->offset = s->offset;
1795 /* show change handled */
1796 debug_immcall = 0;
1800 static void dwarf_generate(void)
1802 uint8_t *pbuf;
1803 int indx;
1804 struct linelist *ftentry;
1805 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1806 struct SAA *parangesrel, *plinesrel, *pinforel;
1807 struct sectlist *psect;
1808 size_t saalen, linepoff, totlen, highaddr;
1810 /* write epilogues for each line program range */
1811 /* and build aranges section */
1812 paranges = saa_init(1L);
1813 parangesrel = saa_init(1L);
1814 saa_write16(paranges,3); /* dwarf version */
1815 saa_write32(parangesrel, paranges->datalen+4);
1816 saa_write32(parangesrel, (dwarf_infosym << 8) + R_X86_64_32); /* reloc to info */
1817 saa_write32(parangesrel, 0);
1818 saa_write32(paranges,0); /* offset into info */
1819 saa_write8(paranges,4); /* pointer size */
1820 saa_write8(paranges,0); /* not segmented */
1821 saa_write32(paranges,0); /* padding */
1822 /* iterate though sectlist entries */
1823 psect = dwarf_fsect;
1824 totlen = 0;
1825 highaddr = 0;
1826 for (indx = 0; indx < dwarf_nsections; indx++)
1828 plinep = psect->psaa;
1829 /* Line Number Program Epilogue */
1830 saa_write8(plinep,2); /* std op 2 */
1831 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1832 saa_write8(plinep,DW_LNS_extended_op);
1833 saa_write8(plinep,1); /* operand length */
1834 saa_write8(plinep,DW_LNE_end_sequence);
1835 totlen += plinep->datalen;
1836 /* range table relocation entry */
1837 saa_write32(parangesrel, paranges->datalen + 4);
1838 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_X86_64_32);
1839 saa_write32(parangesrel, (uint32_t) 0);
1840 /* range table entry */
1841 saa_write32(paranges,0x0000); /* range start */
1842 saa_write32(paranges,sects[psect->section]->len); /* range length */
1843 highaddr += sects[psect->section]->len;
1844 /* done with this entry */
1845 psect = psect->next;
1847 saa_write32(paranges,0); /* null address */
1848 saa_write32(paranges,0); /* null length */
1849 saalen = paranges->datalen;
1850 arangeslen = saalen + 4;
1851 arangesbuf = pbuf = nasm_malloc(arangeslen);
1852 WRITELONG(pbuf,saalen); /* initial length */
1853 saa_rnbytes(paranges, pbuf, saalen);
1854 saa_free(paranges);
1856 /* build rela.aranges section */
1857 arangesrellen = saalen = parangesrel->datalen;
1858 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1859 saa_rnbytes(parangesrel, pbuf, saalen);
1860 saa_free(parangesrel);
1862 /* build pubnames section */
1863 ppubnames = saa_init(1L);
1864 saa_write16(ppubnames,3); /* dwarf version */
1865 saa_write32(ppubnames,0); /* offset into info */
1866 saa_write32(ppubnames,0); /* space used in info */
1867 saa_write32(ppubnames,0); /* end of list */
1868 saalen = ppubnames->datalen;
1869 pubnameslen = saalen + 4;
1870 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1871 WRITELONG(pbuf,saalen); /* initial length */
1872 saa_rnbytes(ppubnames, pbuf, saalen);
1873 saa_free(ppubnames);
1875 /* build info section */
1876 pinfo = saa_init(1L);
1877 pinforel = saa_init(1L);
1878 saa_write16(pinfo,3); /* dwarf version */
1879 saa_write32(pinforel, pinfo->datalen + 4);
1880 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_X86_64_32); /* reloc to abbrev */
1881 saa_write32(pinforel, 0);
1882 saa_write32(pinfo,0); /* offset into abbrev */
1883 saa_write8(pinfo,4); /* pointer size */
1884 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1885 saa_write32(pinforel, pinfo->datalen + 4);
1886 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
1887 saa_write32(pinforel, 0);
1888 saa_write32(pinfo,0); /* DW_AT_low_pc */
1889 saa_write32(pinforel, pinfo->datalen + 4);
1890 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
1891 saa_write32(pinforel, 0);
1892 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
1893 saa_write32(pinforel, pinfo->datalen + 4);
1894 saa_write32(pinforel, (dwarf_linesym << 8) + R_X86_64_32); /* reloc to line */
1895 saa_write32(pinforel, 0);
1896 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1897 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1898 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1899 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1900 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1901 saa_write32(pinforel, pinfo->datalen + 4);
1902 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
1903 saa_write32(pinforel, 0);
1904 saa_write32(pinfo,0); /* DW_AT_low_pc */
1905 saa_write32(pinfo,0); /* DW_AT_frame_base */
1906 saa_write8(pinfo,0); /* end of entries */
1907 saalen = pinfo->datalen;
1908 infolen = saalen + 4;
1909 infobuf = pbuf = nasm_malloc(infolen);
1910 WRITELONG(pbuf,saalen); /* initial length */
1911 saa_rnbytes(pinfo, pbuf, saalen);
1912 saa_free(pinfo);
1914 /* build rela.info section */
1915 inforellen = saalen = pinforel->datalen;
1916 inforelbuf = pbuf = nasm_malloc(inforellen);
1917 saa_rnbytes(pinforel, pbuf, saalen);
1918 saa_free(pinforel);
1920 /* build abbrev section */
1921 pabbrev = saa_init(1L);
1922 saa_write8(pabbrev,1); /* entry number LEB128u */
1923 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
1924 saa_write8(pabbrev,1); /* has children */
1925 /* the following attributes and forms are all LEB128u values */
1926 saa_write8(pabbrev,DW_AT_low_pc);
1927 saa_write8(pabbrev,DW_FORM_addr);
1928 saa_write8(pabbrev,DW_AT_high_pc);
1929 saa_write8(pabbrev,DW_FORM_addr);
1930 saa_write8(pabbrev,DW_AT_stmt_list);
1931 saa_write8(pabbrev,DW_FORM_data4);
1932 saa_write8(pabbrev,DW_AT_name);
1933 saa_write8(pabbrev,DW_FORM_string);
1934 saa_write8(pabbrev,DW_AT_producer);
1935 saa_write8(pabbrev,DW_FORM_string);
1936 saa_write8(pabbrev,DW_AT_language);
1937 saa_write8(pabbrev,DW_FORM_data2);
1938 saa_write16(pabbrev,0); /* end of entry */
1939 /* LEB128u usage same as above */
1940 saa_write8(pabbrev,2); /* entry number */
1941 saa_write8(pabbrev,DW_TAG_subprogram);
1942 saa_write8(pabbrev,0); /* no children */
1943 saa_write8(pabbrev,DW_AT_low_pc);
1944 saa_write8(pabbrev,DW_FORM_addr);
1945 saa_write8(pabbrev,DW_AT_frame_base);
1946 saa_write8(pabbrev,DW_FORM_data4);
1947 saa_write16(pabbrev,0); /* end of entry */
1948 abbrevlen = saalen = pabbrev->datalen;
1949 abbrevbuf = pbuf = nasm_malloc(saalen);
1950 saa_rnbytes(pabbrev, pbuf, saalen);
1951 saa_free(pabbrev);
1953 /* build line section */
1954 /* prolog */
1955 plines = saa_init(1L);
1956 saa_write8(plines,1); /* Minimum Instruction Length */
1957 saa_write8(plines,1); /* Initial value of 'is_stmt' */
1958 saa_write8(plines,line_base); /* Line Base */
1959 saa_write8(plines,line_range); /* Line Range */
1960 saa_write8(plines,opcode_base); /* Opcode Base */
1961 /* standard opcode lengths (# of LEB128u operands) */
1962 saa_write8(plines,0); /* Std opcode 1 length */
1963 saa_write8(plines,1); /* Std opcode 2 length */
1964 saa_write8(plines,1); /* Std opcode 3 length */
1965 saa_write8(plines,1); /* Std opcode 4 length */
1966 saa_write8(plines,1); /* Std opcode 5 length */
1967 saa_write8(plines,0); /* Std opcode 6 length */
1968 saa_write8(plines,0); /* Std opcode 7 length */
1969 saa_write8(plines,0); /* Std opcode 8 length */
1970 saa_write8(plines,1); /* Std opcode 9 length */
1971 saa_write8(plines,0); /* Std opcode 10 length */
1972 saa_write8(plines,0); /* Std opcode 11 length */
1973 saa_write8(plines,1); /* Std opcode 12 length */
1974 /* Directory Table */
1975 saa_write8(plines,0); /* End of table */
1976 /* File Name Table */
1977 ftentry = dwarf_flist;
1978 for (indx = 0;indx<dwarf_numfiles;indx++)
1980 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
1981 saa_write8(plines,0); /* directory LEB128u */
1982 saa_write8(plines,0); /* time LEB128u */
1983 saa_write8(plines,0); /* size LEB128u */
1984 ftentry = ftentry->next;
1986 saa_write8(plines,0); /* End of table */
1987 linepoff = plines->datalen;
1988 linelen = linepoff + totlen + 10;
1989 linebuf = pbuf = nasm_malloc(linelen);
1990 WRITELONG(pbuf,linelen-4); /* initial length */
1991 WRITESHORT(pbuf,3); /* dwarf version */
1992 WRITELONG(pbuf,linepoff); /* offset to line number program */
1993 /* write line header */
1994 saalen = linepoff;
1995 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
1996 pbuf += linepoff;
1997 saa_free(plines);
1998 /* concatonate line program ranges */
1999 linepoff += 13;
2000 plinesrel = saa_init(1L);
2001 psect = dwarf_fsect;
2002 for (indx = 0; indx < dwarf_nsections; indx++) {
2003 saa_write32(plinesrel, linepoff);
2004 saa_write32(plinesrel, ((psect->section + 2) << 8) + R_X86_64_32);
2005 saa_write32(plinesrel, 0);
2006 plinep = psect->psaa;
2007 saalen = plinep->datalen;
2008 saa_rnbytes(plinep, pbuf, saalen);
2009 pbuf += saalen;
2010 linepoff += saalen;
2011 saa_free(plinep);
2012 /* done with this entry */
2013 psect = psect->next;
2017 /* build rela.lines section */
2018 linerellen =saalen = plinesrel->datalen;
2019 linerelbuf = pbuf = nasm_malloc(linerellen);
2020 saa_rnbytes(plinesrel, pbuf, saalen);
2021 saa_free(plinesrel);
2023 /* build frame section */
2024 framelen = 4;
2025 framebuf = pbuf = nasm_malloc(framelen);
2026 WRITELONG(pbuf,framelen-4); /* initial length */
2028 /* build loc section */
2029 loclen = 16;
2030 locbuf = pbuf = nasm_malloc(loclen);
2031 WRITELONG(pbuf,0); /* null beginning offset */
2032 WRITELONG(pbuf,0); /* null ending offset */
2035 static void dwarf_cleanup(void)
2037 nasm_free(arangesbuf);
2038 nasm_free(arangesrelbuf);
2039 nasm_free(pubnamesbuf);
2040 nasm_free(infobuf);
2041 nasm_free(inforelbuf);
2042 nasm_free(abbrevbuf);
2043 nasm_free(linebuf);
2044 nasm_free(linerelbuf);
2045 nasm_free(framebuf);
2046 nasm_free(locbuf);
2049 static void dwarf_findfile(const char * fname)
2051 int finx;
2052 struct linelist *match;
2054 /* return if fname is current file name */
2055 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2056 return;
2058 /* search for match */
2059 match = 0;
2060 if (dwarf_flist) {
2061 match = dwarf_flist;
2062 for (finx = 0; finx < dwarf_numfiles; finx++) {
2063 if (!(strcmp(fname, match->filename))) {
2064 dwarf_clist = match;
2065 return;
2070 /* add file name to end of list */
2071 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2072 dwarf_numfiles++;
2073 dwarf_clist->line = dwarf_numfiles;
2074 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2075 strcpy(dwarf_clist->filename,fname);
2076 dwarf_clist->next = 0;
2077 if (!dwarf_flist) { /* if first entry */
2078 dwarf_flist = dwarf_elist = dwarf_clist;
2079 dwarf_clist->last = 0;
2080 } else { /* chain to previous entry */
2081 dwarf_elist->next = dwarf_clist;
2082 dwarf_elist = dwarf_clist;
2086 static void dwarf_findsect(const int index)
2088 int sinx;
2089 struct sectlist *match;
2090 struct SAA *plinep;
2092 /* return if index is current section index */
2093 if (dwarf_csect && (dwarf_csect->section == index))
2094 return;
2096 /* search for match */
2097 match = 0;
2098 if (dwarf_fsect) {
2099 match = dwarf_fsect;
2100 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2101 if (match->section == index) {
2102 dwarf_csect = match;
2103 return;
2105 match = match->next;
2109 /* add entry to end of list */
2110 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2111 dwarf_nsections++;
2112 dwarf_csect->psaa = plinep = saa_init(1L);
2113 dwarf_csect->line = 1;
2114 dwarf_csect->offset = 0;
2115 dwarf_csect->file = 1;
2116 dwarf_csect->section = index;
2117 dwarf_csect->next = 0;
2118 /* set relocatable address at start of line program */
2119 saa_write8(plinep,DW_LNS_extended_op);
2120 saa_write8(plinep,5); /* operand length */
2121 saa_write8(plinep,DW_LNE_set_address);
2122 saa_write32(plinep,0); /* Start Address */
2124 if (!dwarf_fsect) { /* if first entry */
2125 dwarf_fsect = dwarf_esect = dwarf_csect;
2126 dwarf_csect->last = 0;
2127 } else { /* chain to previous entry */
2128 dwarf_esect->next = dwarf_csect;
2129 dwarf_esect = dwarf_csect;
2133 #endif /* OF_ELFX32 */