output: outmac -- Fix few nits during merge
[nasm.git] / output / outelfx32.c
blobc217c7becd95b7409ecb3d76c1bb9dc6ce2f9245
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2013 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * outelfx32.c output routines for the Netwide Assembler to produce
36 * ELF32 (x86_64) object file format
39 #include "compiler.h"
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <inttypes.h>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "saa.h"
50 #include "raa.h"
51 #include "stdscan.h"
52 #include "eval.h"
53 #include "output/outform.h"
54 #include "output/outlib.h"
55 #include "rbtree.h"
57 #include "output/dwarf.h"
58 #include "output/stabs.h"
59 #include "output/outelf.h"
61 #ifdef OF_ELFX32
63 #define SECT_DELTA 32
64 static struct elf_section **sects;
65 static int nsects, sectlen;
67 #define SHSTR_DELTA 256
68 static char *shstrtab;
69 static int shstrtablen, shstrtabsize;
71 static struct SAA *syms;
72 static uint32_t nlocals, nglobs, ndebugs; /* Symbol counts */
74 static int32_t def_seg;
76 static struct RAA *bsym;
78 static struct SAA *strs;
79 static uint32_t strslen;
81 static struct elf_symbol *fwds;
83 static char elf_module[FILENAME_MAX];
85 extern struct ofmt of_elfx32;
87 static struct ELF_SECTDATA {
88 void *data;
89 int32_t len;
90 bool is_saa;
91 } *elf_sects;
92 static int elf_nsect, nsections;
93 static int32_t elf_foffs;
95 static void elf_write(void);
96 static void elf_sect_write(struct elf_section *, const void *, size_t);
97 static void elf_sect_writeaddr(struct elf_section *, int32_t, size_t);
98 static void elf_section_header(int, int, uint32_t, void *, bool, uint32_t, int, int,
99 int, int);
100 static void elf_write_sections(void);
101 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
102 static struct SAA *elf_build_reltab(uint64_t *, struct elf_reloc *);
103 static void add_sectname(char *, char *);
105 struct erel {
106 int offset, info;
109 struct symlininfo {
110 int offset;
111 int section; /* index into sects[] */
112 int segto; /* internal section number */
113 char *name; /* shallow-copied pointer of section name */
116 struct linelist {
117 struct linelist *next;
118 struct linelist *last;
119 struct symlininfo info;
120 char *filename;
121 int line;
124 struct sectlist {
125 struct SAA *psaa;
126 int section;
127 int line;
128 int offset;
129 int file;
130 struct sectlist *next;
131 struct sectlist *last;
134 /* common debug variables */
135 static int currentline = 1;
136 static int debug_immcall = 0;
138 /* stabs debug variables */
139 static struct linelist *stabslines = 0;
140 static int numlinestabs = 0;
141 static char *stabs_filename = 0;
142 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
143 static int stablen, stabstrlen, stabrellen;
145 /* dwarf debug variables */
146 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
147 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
148 static int dwarf_numfiles = 0, dwarf_nsections;
149 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
150 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
151 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
152 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
153 abbrevlen, linelen, linerellen, framelen, loclen;
154 static int32_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
157 static struct dfmt df_dwarf;
158 static struct dfmt df_stabs;
159 static struct elf_symbol *lastsym;
161 /* common debugging routines */
162 static void debugx32_typevalue(int32_t);
163 static void debugx32_deflabel(char *, int32_t, int64_t, int, char *);
164 static void debugx32_directive(const char *, const char *);
166 /* stabs debugging routines */
167 static void stabsx32_linenum(const char *filename, int32_t linenumber, int32_t);
168 static void stabsx32_output(int, void *);
169 static void stabsx32_generate(void);
170 static void stabsx32_cleanup(void);
172 /* dwarf debugging routines */
173 static void dwarfx32_init(void);
174 static void dwarfx32_linenum(const char *filename, int32_t linenumber, int32_t);
175 static void dwarfx32_output(int, void *);
176 static void dwarfx32_generate(void);
177 static void dwarfx32_cleanup(void);
178 static void dwarfx32_findfile(const char *);
179 static void dwarfx32_findsect(const int);
182 * Special section numbers which are used to define ELF special
183 * symbols, which can be used with WRT to provide PIC relocation
184 * types.
186 static int32_t elf_gotpc_sect, elf_gotoff_sect;
187 static int32_t elf_got_sect, elf_plt_sect;
188 static int32_t elf_sym_sect;
189 static int32_t elf_gottpoff_sect;
191 static void elf_init(void)
193 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 (of_elfx32.current_dfmt) {
249 of_elfx32.current_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, nasm_error, 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_error(ERR_PANIC, "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, nasm_error,
533 NULL);
534 if (fwd) {
535 sym->nextfwd = fwds;
536 fwds = sym;
537 sym->name = nasm_strdup(name);
538 } else if (e) {
539 if (!is_simple(e))
540 nasm_error(ERR_NONFATAL, "cannot use relocatable"
541 " expression as symbol size");
542 else
543 sym->size = reloc_value(e);
545 stdscan_set(saveme);
547 special_used = true;
550 * If TLS segment, mark symbol accordingly.
552 if (sects[sym->section - 1]->flags & SHF_TLS) {
553 sym->type &= 0xf0;
554 sym->type |= STT_TLS;
557 sym->globnum = nglobs;
558 nglobs++;
559 } else
560 nlocals++;
562 if (special && !special_used)
563 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
566 static void elf_add_reloc(struct elf_section *sect, int32_t segment,
567 int32_t offset, int type)
569 struct elf_reloc *r;
571 r = *sect->tail = nasm_zalloc(sizeof(struct elf_reloc));
572 sect->tail = &r->next;
574 r->address = sect->len;
575 r->offset = offset;
577 if (segment != NO_SEG) {
578 int i;
579 for (i = 0; i < nsects; i++)
580 if (segment == sects[i]->index)
581 r->symbol = i + 2;
582 if (!r->symbol)
583 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
585 r->type = type;
587 sect->nrelocs++;
591 * This routine deals with ..got and ..sym relocations: the more
592 * complicated kinds. In shared-library writing, some relocations
593 * with respect to global symbols must refer to the precise symbol
594 * rather than referring to an offset from the base of the section
595 * _containing_ the symbol. Such relocations call to this routine,
596 * which searches the symbol list for the symbol in question.
598 * R_X86_64_GOT32 references require the _exact_ symbol address to be
599 * used; R_X86_64_32 references can be at an offset from the symbol.
600 * The boolean argument `exact' tells us this.
602 * Return value is the adjusted value of `addr', having become an
603 * offset from the symbol rather than the section. Should always be
604 * zero when returning from an exact call.
606 * Limitation: if you define two symbols at the same place,
607 * confusion will occur.
609 * Inefficiency: we search, currently, using a linked list which
610 * isn't even necessarily sorted.
612 static void elf_add_gsym_reloc(struct elf_section *sect,
613 int32_t segment, uint32_t offset, int32_t pcrel,
614 int type, bool exact)
616 struct elf_reloc *r;
617 struct elf_section *s;
618 struct elf_symbol *sym;
619 struct rbtree *srb;
620 int i;
623 * First look up the segment/offset pair and find a global
624 * symbol corresponding to it. If it's not one of our segments,
625 * then it must be an external symbol, in which case we're fine
626 * doing a normal elf_add_reloc after first sanity-checking
627 * that the offset from the symbol is zero.
629 s = NULL;
630 for (i = 0; i < nsects; i++)
631 if (segment == sects[i]->index) {
632 s = sects[i];
633 break;
636 if (!s) {
637 if (exact && offset)
638 nasm_error(ERR_NONFATAL, "invalid access to an external symbol");
639 else
640 elf_add_reloc(sect, segment, offset - pcrel, type);
641 return;
644 srb = rb_search(s->gsyms, offset);
645 if (!srb || (exact && srb->key != offset)) {
646 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
647 " for this reference");
648 return;
650 sym = container_of(srb, struct elf_symbol, symv);
652 r = *sect->tail = nasm_malloc(sizeof(struct elf_reloc));
653 sect->tail = &r->next;
654 r->next = NULL;
656 r->address = sect->len;
657 r->offset = offset - pcrel - sym->symv.key;
658 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
659 r->type = type;
661 sect->nrelocs++;
664 static void elf_out(int32_t segto, const void *data,
665 enum out_type type, uint64_t size,
666 int32_t segment, int32_t wrt)
668 struct elf_section *s;
669 int32_t addr;
670 int reltype, bytes;
671 int i;
672 static struct symlininfo sinfo;
674 #if defined(DEBUG) && DEBUG>2
675 if (data)
676 nasm_error(ERR_DEBUG,
677 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64" data: %"PRIx64"\n",
678 currentline, type, segment, segto, size, *(int64_t *)data);
679 else
680 nasm_error(ERR_DEBUG,
681 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64"\n",
682 currentline, type, segment, segto, size);
683 #endif
686 * handle absolute-assembly (structure definitions)
688 if (segto == NO_SEG) {
689 if (type != OUT_RESERVE)
690 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
691 " space");
692 return;
695 s = NULL;
696 for (i = 0; i < nsects; i++)
697 if (segto == sects[i]->index) {
698 s = sects[i];
699 break;
701 if (!s) {
702 int tempint; /* ignored */
703 if (segto != elf_section_names(".text", 2, &tempint))
704 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
705 else {
706 s = sects[nsects - 1];
707 i = nsects - 1;
711 /* again some stabs debugging stuff */
712 if (of_elfx32.current_dfmt) {
713 sinfo.offset = s->len;
714 sinfo.section = i;
715 sinfo.segto = segto;
716 sinfo.name = s->name;
717 of_elfx32.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
719 /* end of debugging stuff */
721 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
722 nasm_error(ERR_WARNING, "attempt to initialize memory in"
723 " BSS section `%s': ignored", s->name);
724 s->len += realsize(type, size);
725 return;
728 switch (type) {
729 case OUT_RESERVE:
730 if (s->type == SHT_PROGBITS) {
731 nasm_error(ERR_WARNING, "uninitialized space declared in"
732 " non-BSS section `%s': zeroing", s->name);
733 elf_sect_write(s, NULL, size);
734 } else
735 s->len += size;
736 break;
738 case OUT_RAWDATA:
739 if (segment != NO_SEG)
740 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
741 elf_sect_write(s, data, size);
742 break;
744 case OUT_ADDRESS:
746 int isize = (int)size;
747 int asize = abs((int)size);
749 addr = *(int64_t *)data;
750 if (segment == NO_SEG) {
751 /* Do nothing */
752 } else if (segment % 2) {
753 nasm_error(ERR_NONFATAL, "ELF format does not support"
754 " segment base references");
755 } else {
756 if (wrt == NO_SEG) {
757 switch (isize) {
758 case 1:
759 case -1:
760 elf_add_reloc(s, segment, addr, R_X86_64_8);
761 break;
762 case 2:
763 case -2:
764 elf_add_reloc(s, segment, addr, R_X86_64_16);
765 break;
766 case 4:
767 elf_add_reloc(s, segment, addr, R_X86_64_32);
768 break;
769 case -4:
770 elf_add_reloc(s, segment, addr, R_X86_64_32S);
771 break;
772 case 8:
773 case -8:
774 elf_add_reloc(s, segment, addr, R_X86_64_64);
775 break;
776 default:
777 nasm_error(ERR_PANIC, "internal error elfx32-hpa-871");
778 break;
780 addr = 0;
781 } else if (wrt == elf_gotpc_sect + 1) {
783 * The user will supply GOT relative to $$. ELF
784 * will let us have GOT relative to $. So we
785 * need to fix up the data item by $-$$.
787 addr += s->len;
788 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
789 addr = 0;
790 } else if (wrt == elf_gotoff_sect + 1) {
791 nasm_error(ERR_NONFATAL, "ELFX32 doesn't support "
792 "R_X86_64_GOTOFF64");
793 } else if (wrt == elf_got_sect + 1) {
794 switch (asize) {
795 case 4:
796 elf_add_gsym_reloc(s, segment, addr, 0,
797 R_X86_64_GOT32, true);
798 addr = 0;
799 break;
800 default:
801 nasm_error(ERR_NONFATAL, "invalid ..got reference");
802 break;
804 } else if (wrt == elf_sym_sect + 1) {
805 switch (isize) {
806 case 1:
807 case -1:
808 elf_add_gsym_reloc(s, segment, addr, 0,
809 R_X86_64_8, false);
810 addr = 0;
811 break;
812 case 2:
813 case -2:
814 elf_add_gsym_reloc(s, segment, addr, 0,
815 R_X86_64_16, false);
816 addr = 0;
817 break;
818 case 4:
819 elf_add_gsym_reloc(s, segment, addr, 0,
820 R_X86_64_32, false);
821 addr = 0;
822 break;
823 case -4:
824 elf_add_gsym_reloc(s, segment, addr, 0,
825 R_X86_64_32S, false);
826 addr = 0;
827 break;
828 case 8:
829 case -8:
830 elf_add_gsym_reloc(s, segment, addr, 0,
831 R_X86_64_64, false);
832 addr = 0;
833 break;
834 default:
835 nasm_error(ERR_PANIC, "internal error elfx32-hpa-903");
836 break;
838 } else if (wrt == elf_plt_sect + 1) {
839 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
840 "relative PLT references");
841 } else {
842 nasm_error(ERR_NONFATAL, "ELF format does not support this"
843 " use of WRT");
846 elf_sect_writeaddr(s, addr, asize);
847 break;
850 case OUT_REL1ADR:
851 reltype = R_X86_64_PC8;
852 bytes = 1;
853 goto rel12adr;
855 case OUT_REL2ADR:
856 reltype = R_X86_64_PC16;
857 bytes = 2;
858 goto rel12adr;
860 rel12adr:
861 addr = *(int64_t *)data - size;
862 if (segment == segto)
863 nasm_error(ERR_PANIC, "intra-segment OUT_REL1ADR");
864 if (segment == NO_SEG) {
865 /* Do nothing */
866 } else if (segment % 2) {
867 nasm_error(ERR_NONFATAL, "ELF format does not support"
868 " segment base references");
869 } else {
870 if (wrt == NO_SEG) {
871 elf_add_reloc(s, segment, addr, reltype);
872 addr = 0;
873 } else {
874 nasm_error(ERR_NONFATAL,
875 "Unsupported non-32-bit ELF relocation");
878 elf_sect_writeaddr(s, addr, bytes);
879 break;
881 case OUT_REL4ADR:
882 addr = *(int64_t *)data - size;
883 if (segment == segto)
884 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
885 if (segment == NO_SEG) {
886 /* Do nothing */
887 } else if (segment % 2) {
888 nasm_error(ERR_NONFATAL, "ELFX32 format does not support"
889 " segment base references");
890 } else {
891 if (wrt == NO_SEG) {
892 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
893 addr = 0;
894 } else if (wrt == elf_plt_sect + 1) {
895 elf_add_gsym_reloc(s, segment, addr+size, size,
896 R_X86_64_PLT32, true);
897 addr = 0;
898 } else if (wrt == elf_gotpc_sect + 1 ||
899 wrt == elf_got_sect + 1) {
900 elf_add_gsym_reloc(s, segment, addr+size, size,
901 R_X86_64_GOTPCREL, true);
902 addr = 0;
903 } else if (wrt == elf_gotoff_sect + 1 ||
904 wrt == elf_got_sect + 1) {
905 nasm_error(ERR_NONFATAL, "invalid ..gotoff reference");
906 } else if (wrt == elf_gottpoff_sect + 1) {
907 elf_add_gsym_reloc(s, segment, addr+size, size,
908 R_X86_64_GOTTPOFF, true);
909 addr = 0;
910 } else {
911 nasm_error(ERR_NONFATAL, "ELFX32 format does not support this"
912 " use of WRT");
915 elf_sect_writeaddr(s, addr, 4);
916 break;
918 case OUT_REL8ADR:
919 nasm_error(ERR_NONFATAL,
920 "32-bit ELF format does not support 64-bit relocations");
921 addr = 0;
922 elf_sect_writeaddr(s, addr, 8);
923 break;
927 static void elf_write(void)
929 int align;
930 char *p;
931 int i;
933 struct SAA *symtab;
934 int32_t symtablen, symtablocal;
937 * Work out how many sections we will have. We have SHN_UNDEF,
938 * then the flexible user sections, then the fixed sections
939 * `.shstrtab', `.symtab' and `.strtab', then optionally
940 * relocation sections for the user sections.
942 nsections = sec_numspecial + 1;
943 if (of_elfx32.current_dfmt == &df_stabs)
944 nsections += 3;
945 else if (of_elfx32.current_dfmt == &df_dwarf)
946 nsections += 10;
948 add_sectname("", ".shstrtab");
949 add_sectname("", ".symtab");
950 add_sectname("", ".strtab");
951 for (i = 0; i < nsects; i++) {
952 nsections++; /* for the section itself */
953 if (sects[i]->head) {
954 nsections++; /* for its relocations */
955 add_sectname(".rela", sects[i]->name);
959 if (of_elfx32.current_dfmt == &df_stabs) {
960 /* in case the debug information is wanted, just add these three sections... */
961 add_sectname("", ".stab");
962 add_sectname("", ".stabstr");
963 add_sectname(".rel", ".stab");
966 else if (of_elfx32.current_dfmt == &df_dwarf) {
967 /* the dwarf debug standard specifies the following ten sections,
968 not all of which are currently implemented,
969 although all of them are defined. */
970 add_sectname("", ".debug_aranges");
971 add_sectname(".rela", ".debug_aranges");
972 add_sectname("", ".debug_pubnames");
973 add_sectname("", ".debug_info");
974 add_sectname(".rela", ".debug_info");
975 add_sectname("", ".debug_abbrev");
976 add_sectname("", ".debug_line");
977 add_sectname(".rela", ".debug_line");
978 add_sectname("", ".debug_frame");
979 add_sectname("", ".debug_loc");
983 * Output the ELF header.
985 nasm_write("\177ELF\1\1\1", 7, ofile);
986 fputc(elf_osabi, ofile);
987 fputc(elf_abiver, ofile);
988 fwritezero(7, ofile);
989 fwriteint16_t(ET_REL, ofile); /* relocatable file */
990 fwriteint16_t(EM_X86_64, ofile); /* processor ID */
991 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
992 fwriteint32_t(0L, ofile); /* no entry point */
993 fwriteint32_t(0L, ofile); /* no program header table */
994 fwriteint32_t(0x40L, ofile); /* section headers straight after
995 * ELF header plus alignment */
996 fwriteint32_t(0L, ofile); /* X86_64 defines no special flags */
997 fwriteint16_t(0x34, ofile); /* size of ELF header */
998 fwriteint16_t(0, ofile); /* no program header table, again */
999 fwriteint16_t(0, ofile); /* still no program header table */
1000 fwriteint16_t(sizeof(Elf32_Shdr), ofile); /* size of section header */
1001 fwriteint16_t(nsections, ofile); /* number of sections */
1002 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
1003 * section header table */
1004 fwriteint32_t(0L, ofile); /* align to 0x40 bytes */
1005 fwriteint32_t(0L, ofile);
1006 fwriteint32_t(0L, ofile);
1009 * Build the symbol table and relocation tables.
1011 symtab = elf_build_symtab(&symtablen, &symtablocal);
1012 for (i = 0; i < nsects; i++)
1013 if (sects[i]->head)
1014 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1015 sects[i]->head);
1018 * Now output the section header table.
1021 elf_foffs = 0x40 + sizeof(Elf32_Shdr) * nsections;
1022 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1023 elf_foffs += align;
1024 elf_nsect = 0;
1025 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1027 /* SHN_UNDEF */
1028 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1029 p = shstrtab + 1;
1031 /* The normal sections */
1032 for (i = 0; i < nsects; i++) {
1033 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1034 (sects[i]->type == SHT_PROGBITS ?
1035 sects[i]->data : NULL), true,
1036 sects[i]->len, 0, 0, sects[i]->align, 0);
1037 p += strlen(p) + 1;
1040 /* .shstrtab */
1041 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1042 shstrtablen, 0, 0, 1, 0);
1043 p += strlen(p) + 1;
1045 /* .symtab */
1046 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1047 symtablen, sec_strtab, symtablocal, 4, 16);
1048 p += strlen(p) + 1;
1050 /* .strtab */
1051 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1052 strslen, 0, 0, 1, 0);
1053 p += strlen(p) + 1;
1055 /* The relocation sections */
1056 for (i = 0; i < nsects; i++)
1057 if (sects[i]->head) {
1058 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1059 sects[i]->rellen, sec_symtab, i + 1, 4, 12);
1060 p += strlen(p) + 1;
1063 if (of_elfx32.current_dfmt == &df_stabs) {
1064 /* for debugging information, create the last three sections
1065 which are the .stab , .stabstr and .rel.stab sections respectively */
1067 /* this function call creates the stab sections in memory */
1068 stabsx32_generate();
1070 if (stabbuf && stabstrbuf && stabrelbuf) {
1071 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1072 stablen, sec_stabstr, 0, 4, 12);
1073 p += strlen(p) + 1;
1075 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1076 stabstrlen, 0, 0, 4, 0);
1077 p += strlen(p) + 1;
1079 /* link -> symtable info -> section to refer to */
1080 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1081 stabrellen, sec_symtab, sec_stab, 4, 8);
1082 p += strlen(p) + 1;
1084 } else if (of_elfx32.current_dfmt == &df_dwarf) {
1085 /* for dwarf debugging information, create the ten dwarf sections */
1087 /* this function call creates the dwarf sections in memory */
1088 if (dwarf_fsect)
1089 dwarfx32_generate();
1091 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1092 arangeslen, 0, 0, 1, 0);
1093 p += strlen(p) + 1;
1095 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1096 arangesrellen, sec_symtab, sec_debug_aranges, 1, 12);
1097 p += strlen(p) + 1;
1099 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1100 pubnameslen, 0, 0, 1, 0);
1101 p += strlen(p) + 1;
1103 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1104 infolen, 0, 0, 1, 0);
1105 p += strlen(p) + 1;
1107 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1108 inforellen, sec_symtab, sec_debug_info, 1, 12);
1109 p += strlen(p) + 1;
1111 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1112 abbrevlen, 0, 0, 1, 0);
1113 p += strlen(p) + 1;
1115 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1116 linelen, 0, 0, 1, 0);
1117 p += strlen(p) + 1;
1119 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1120 linerellen, sec_symtab, sec_debug_line, 1, 12);
1121 p += strlen(p) + 1;
1123 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1124 framelen, 0, 0, 8, 0);
1125 p += strlen(p) + 1;
1127 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1128 loclen, 0, 0, 1, 0);
1129 p += strlen(p) + 1;
1131 fwritezero(align, ofile);
1134 * Now output the sections.
1136 elf_write_sections();
1138 nasm_free(elf_sects);
1139 saa_free(symtab);
1142 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1144 struct SAA *s = saa_init(1L);
1145 struct elf_symbol *sym;
1146 uint8_t entry[24], *p;
1147 int i;
1149 *len = *local = 0;
1152 * First, an all-zeros entry, required by the ELF spec.
1154 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1155 *len += 16;
1156 (*local)++;
1159 * Next, an entry for the file name.
1161 p = entry;
1162 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1163 WRITELONG(p, 0); /* no value */
1164 WRITELONG(p, 0); /* no size either */
1165 WRITESHORT(p, STT_FILE); /* type FILE */
1166 WRITESHORT(p, SHN_ABS);
1167 saa_wbytes(s, entry, 16L);
1168 *len += 16;
1169 (*local)++;
1172 * Now some standard symbols defining the segments, for relocation
1173 * purposes.
1175 for (i = 1; i <= nsects; i++) {
1176 p = entry;
1177 WRITELONG(p, 0); /* no symbol name */
1178 WRITELONG(p, 0); /* offset zero */
1179 WRITELONG(p, 0); /* size zero */
1180 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1181 WRITESHORT(p, i); /* section id */
1182 saa_wbytes(s, entry, 16L);
1183 *len += 16;
1184 (*local)++;
1189 * Now the other local symbols.
1191 saa_rewind(syms);
1192 while ((sym = saa_rstruct(syms))) {
1193 if (sym->type & SYM_GLOBAL)
1194 continue;
1195 p = entry;
1196 WRITELONG(p, sym->strpos); /* index into symbol string table */
1197 WRITELONG(p, sym->symv.key); /* value of symbol */
1198 WRITELONG(p, sym->size); /* size of symbol */
1199 WRITECHAR(p, sym->type); /* type and binding */
1200 WRITECHAR(p, sym->other); /* visibility */
1201 WRITESHORT(p, sym->section); /* index into section header table */
1202 saa_wbytes(s, entry, 16L);
1203 *len += 16;
1204 (*local)++;
1207 * dwarf needs symbols for debug sections
1208 * which are relocation targets.
1210 if (of_elfx32.current_dfmt == &df_dwarf) {
1211 dwarf_infosym = *local;
1212 p = entry;
1213 WRITELONG(p, 0); /* no symbol name */
1214 WRITELONG(p, 0); /* offset zero */
1215 WRITELONG(p, 0); /* size zero */
1216 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1217 WRITESHORT(p, sec_debug_info); /* section id */
1218 saa_wbytes(s, entry, 16L);
1219 *len += 16;
1220 (*local)++;
1221 dwarf_abbrevsym = *local;
1222 p = entry;
1223 WRITELONG(p, 0); /* no symbol name */
1224 WRITELONG(p, 0); /* offset zero */
1225 WRITELONG(p, 0); /* size zero */
1226 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1227 WRITESHORT(p, sec_debug_abbrev); /* section id */
1228 saa_wbytes(s, entry, 16L);
1229 *len += 16;
1230 (*local)++;
1231 dwarf_linesym = *local;
1232 p = entry;
1233 WRITELONG(p, 0); /* no symbol name */
1234 WRITELONG(p, 0); /* offset zero */
1235 WRITELONG(p, 0); /* size zero */
1236 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1237 WRITESHORT(p, sec_debug_line); /* section id */
1238 saa_wbytes(s, entry, 16L);
1239 *len += 16;
1240 (*local)++;
1244 * Now the global symbols.
1246 saa_rewind(syms);
1247 while ((sym = saa_rstruct(syms))) {
1248 if (!(sym->type & SYM_GLOBAL))
1249 continue;
1250 p = entry;
1251 WRITELONG(p, sym->strpos);
1252 WRITELONG(p, sym->symv.key);
1253 WRITELONG(p, sym->size);
1254 WRITECHAR(p, sym->type); /* type and binding */
1255 WRITECHAR(p, sym->other); /* visibility */
1256 WRITESHORT(p, sym->section);
1257 saa_wbytes(s, entry, 16L);
1258 *len += 16;
1261 return s;
1264 static struct SAA *elf_build_reltab(uint64_t *len, struct elf_reloc *r)
1266 struct SAA *s;
1267 uint8_t *p, entry[12];
1268 int32_t global_offset;
1270 if (!r)
1271 return NULL;
1273 s = saa_init(1L);
1274 *len = 0;
1277 * How to onvert from a global placeholder to a real symbol index;
1278 * the +2 refers to the two special entries, the null entry and
1279 * the filename entry.
1281 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1283 while (r) {
1284 int32_t sym = r->symbol;
1286 if (sym >= GLOBAL_TEMP_BASE)
1287 sym += global_offset;
1289 p = entry;
1290 WRITELONG(p, r->address);
1291 WRITELONG(p, (sym << 8) + r->type);
1292 WRITELONG(p, r->offset);
1293 saa_wbytes(s, entry, 12L);
1294 *len += 12;
1296 r = r->next;
1299 return s;
1302 static void elf_section_header(int name, int type, uint32_t flags,
1303 void *data, bool is_saa, uint32_t datalen,
1304 int link, int info, int align, int eltsize)
1306 elf_sects[elf_nsect].data = data;
1307 elf_sects[elf_nsect].len = datalen;
1308 elf_sects[elf_nsect].is_saa = is_saa;
1309 elf_nsect++;
1311 fwriteint32_t((int32_t)name, ofile);
1312 fwriteint32_t((int32_t)type, ofile);
1313 fwriteint32_t((int32_t)flags, ofile);
1314 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
1315 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
1316 fwriteint32_t(datalen, ofile);
1317 if (data)
1318 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
1319 fwriteint32_t((int32_t)link, ofile);
1320 fwriteint32_t((int32_t)info, ofile);
1321 fwriteint32_t((int32_t)align, ofile);
1322 fwriteint32_t((int32_t)eltsize, ofile);
1325 static void elf_write_sections(void)
1327 int i;
1328 for (i = 0; i < elf_nsect; i++)
1329 if (elf_sects[i].data) {
1330 int32_t len = elf_sects[i].len;
1331 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
1332 int32_t align = reallen - len;
1333 if (elf_sects[i].is_saa)
1334 saa_fpwrite(elf_sects[i].data, ofile);
1335 else
1336 nasm_write(elf_sects[i].data, len, ofile);
1337 fwritezero(align, ofile);
1341 static void elf_sect_write(struct elf_section *sect, const void *data, size_t len)
1343 saa_wbytes(sect->data, data, len);
1344 sect->len += len;
1346 static void elf_sect_writeaddr(struct elf_section *sect, int32_t data, size_t len)
1348 saa_writeaddr(sect->data, data, len);
1349 sect->len += len;
1352 static void elf_sectalign(int32_t seg, unsigned int value)
1354 struct elf_section *s = NULL;
1355 int i;
1357 for (i = 0; i < nsects; i++) {
1358 if (sects[i]->index == seg) {
1359 s = sects[i];
1360 break;
1363 if (!s || !is_power2(value))
1364 return;
1366 if (value > s->align)
1367 s->align = value;
1370 static int32_t elf_segbase(int32_t segment)
1372 return segment;
1375 static void elf_filename(char *inname, char *outname)
1377 strcpy(elf_module, inname);
1378 standard_extension(inname, outname, ".o");
1381 extern macros_t elf_stdmac[];
1383 static int elf_set_info(enum geninfo type, char **val)
1385 (void)type;
1386 (void)val;
1387 return 0;
1389 static struct dfmt df_dwarf = {
1390 "ELFX32 (x86-64) dwarf debug format for Linux/Unix",
1391 "dwarf",
1392 dwarfx32_init,
1393 dwarfx32_linenum,
1394 debugx32_deflabel,
1395 debugx32_directive,
1396 debugx32_typevalue,
1397 dwarfx32_output,
1398 dwarfx32_cleanup
1400 static struct dfmt df_stabs = {
1401 "ELFX32 (x86-64) stabs debug format for Linux/Unix",
1402 "stabs",
1403 null_debug_init,
1404 stabsx32_linenum,
1405 debugx32_deflabel,
1406 debugx32_directive,
1407 debugx32_typevalue,
1408 stabsx32_output,
1409 stabsx32_cleanup
1412 struct dfmt *elfx32_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1414 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 debugx32_deflabel(char *name, int32_t segment, int64_t offset,
1436 int is_global, char *special)
1438 (void)name;
1439 (void)segment;
1440 (void)offset;
1441 (void)is_global;
1442 (void)special;
1445 static void debugx32_directive(const char *directive, const char *params)
1447 (void)directive;
1448 (void)params;
1451 static void debugx32_typevalue(int32_t type)
1453 int32_t stype, ssize;
1454 switch (TYM_TYPE(type)) {
1455 case TY_LABEL:
1456 ssize = 0;
1457 stype = STT_NOTYPE;
1458 break;
1459 case TY_BYTE:
1460 ssize = 1;
1461 stype = STT_OBJECT;
1462 break;
1463 case TY_WORD:
1464 ssize = 2;
1465 stype = STT_OBJECT;
1466 break;
1467 case TY_DWORD:
1468 ssize = 4;
1469 stype = STT_OBJECT;
1470 break;
1471 case TY_FLOAT:
1472 ssize = 4;
1473 stype = STT_OBJECT;
1474 break;
1475 case TY_QWORD:
1476 ssize = 8;
1477 stype = STT_OBJECT;
1478 break;
1479 case TY_TBYTE:
1480 ssize = 10;
1481 stype = STT_OBJECT;
1482 break;
1483 case TY_OWORD:
1484 ssize = 16;
1485 stype = STT_OBJECT;
1486 break;
1487 case TY_YWORD:
1488 ssize = 32;
1489 stype = STT_OBJECT;
1490 break;
1491 case TY_COMMON:
1492 ssize = 0;
1493 stype = STT_COMMON;
1494 break;
1495 case TY_SEG:
1496 ssize = 0;
1497 stype = STT_SECTION;
1498 break;
1499 case TY_EXTERN:
1500 ssize = 0;
1501 stype = STT_NOTYPE;
1502 break;
1503 case TY_EQU:
1504 ssize = 0;
1505 stype = STT_NOTYPE;
1506 break;
1507 default:
1508 ssize = 0;
1509 stype = STT_NOTYPE;
1510 break;
1512 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1513 lastsym->size = ssize;
1514 lastsym->type = stype;
1518 /* stabs debugging routines */
1520 static void stabsx32_linenum(const char *filename, int32_t linenumber, int32_t segto)
1522 (void)segto;
1523 if (!stabs_filename) {
1524 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1525 strcpy(stabs_filename, filename);
1526 } else {
1527 if (strcmp(stabs_filename, filename)) {
1528 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1529 in fact, this leak comes in quite handy to maintain a list of files
1530 encountered so far in the symbol lines... */
1532 /* why not nasm_free(stabs_filename); we're done with the old one */
1534 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1535 strcpy(stabs_filename, filename);
1538 debug_immcall = 1;
1539 currentline = linenumber;
1543 static void stabsx32_output(int type, void *param)
1545 struct symlininfo *s;
1546 struct linelist *el;
1547 if (type == TY_DEBUGSYMLIN) {
1548 if (debug_immcall) {
1549 s = (struct symlininfo *)param;
1550 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1551 return; /* line info is only collected for executable sections */
1552 numlinestabs++;
1553 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1554 el->info.offset = s->offset;
1555 el->info.section = s->section;
1556 el->info.name = s->name;
1557 el->line = currentline;
1558 el->filename = stabs_filename;
1559 el->next = 0;
1560 if (stabslines) {
1561 stabslines->last->next = el;
1562 stabslines->last = el;
1563 } else {
1564 stabslines = el;
1565 stabslines->last = el;
1569 debug_immcall = 0;
1572 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1574 static void stabsx32_generate(void)
1576 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1577 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1578 char **allfiles;
1579 int *fileidx;
1581 struct linelist *ptr;
1583 ptr = stabslines;
1585 allfiles = (char **)nasm_zalloc(numlinestabs * sizeof(char *));
1586 numfiles = 0;
1587 while (ptr) {
1588 if (numfiles == 0) {
1589 allfiles[0] = ptr->filename;
1590 numfiles++;
1591 } else {
1592 for (i = 0; i < numfiles; i++) {
1593 if (!strcmp(allfiles[i], ptr->filename))
1594 break;
1596 if (i >= numfiles) {
1597 allfiles[i] = ptr->filename;
1598 numfiles++;
1601 ptr = ptr->next;
1603 strsize = 1;
1604 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1605 for (i = 0; i < numfiles; i++) {
1606 fileidx[i] = strsize;
1607 strsize += strlen(allfiles[i]) + 1;
1609 mainfileindex = 0;
1610 for (i = 0; i < numfiles; i++) {
1611 if (!strcmp(allfiles[i], elf_module)) {
1612 mainfileindex = i;
1613 break;
1618 * worst case size of the stab buffer would be:
1619 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1620 * plus one "ending" entry
1622 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1623 sizeof(struct stabentry));
1624 ssbuf = (uint8_t *)nasm_malloc(strsize);
1625 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1626 rptr = rbuf;
1628 for (i = 0; i < numfiles; i++)
1629 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1630 ssbuf[0] = 0;
1632 stabstrlen = strsize; /* set global variable for length of stab strings */
1634 sptr = sbuf;
1635 ptr = stabslines;
1636 numstabs = 0;
1638 if (ptr) {
1640 * this is the first stab, its strx points to the filename of the
1641 * the source-file, the n_desc field should be set to the number
1642 * of remaining stabs
1644 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, stabstrlen);
1646 /* this is the stab for the main source file */
1647 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1649 /* relocation table entry */
1652 * Since the symbol table has two entries before
1653 * the section symbols, the index in the info.section
1654 * member must be adjusted by adding 2
1657 WRITELONG(rptr, (sptr - sbuf) - 4);
1658 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
1660 numstabs++;
1661 currfile = mainfileindex;
1664 while (ptr) {
1665 if (strcmp(allfiles[currfile], ptr->filename)) {
1666 /* oops file has changed... */
1667 for (i = 0; i < numfiles; i++)
1668 if (!strcmp(allfiles[i], ptr->filename))
1669 break;
1670 currfile = i;
1671 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1672 ptr->info.offset);
1673 numstabs++;
1675 /* relocation table entry */
1677 WRITELONG(rptr, (sptr - sbuf) - 4);
1678 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
1681 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1682 numstabs++;
1684 /* relocation table entry */
1686 WRITELONG(rptr, (sptr - sbuf) - 4);
1687 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
1689 ptr = ptr->next;
1693 /* this is an "ending" token */
1694 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1695 numstabs++;
1697 ((struct stabentry *)sbuf)->n_desc = numstabs;
1699 nasm_free(allfiles);
1700 nasm_free(fileidx);
1702 stablen = (sptr - sbuf);
1703 stabrellen = (rptr - rbuf);
1704 stabrelbuf = rbuf;
1705 stabbuf = sbuf;
1706 stabstrbuf = ssbuf;
1709 static void stabsx32_cleanup(void)
1711 struct linelist *ptr, *del;
1712 if (!stabslines)
1713 return;
1715 ptr = stabslines;
1716 while (ptr) {
1717 del = ptr;
1718 ptr = ptr->next;
1719 nasm_free(del);
1722 nasm_free(stabbuf);
1723 nasm_free(stabrelbuf);
1724 nasm_free(stabstrbuf);
1727 /* dwarf routines */
1729 static void dwarfx32_init(void)
1731 ndebugs = 3; /* 3 debug symbols */
1734 static void dwarfx32_linenum(const char *filename, int32_t linenumber,
1735 int32_t segto)
1737 (void)segto;
1738 dwarfx32_findfile(filename);
1739 debug_immcall = 1;
1740 currentline = linenumber;
1743 /* called from elf_out with type == TY_DEBUGSYMLIN */
1744 static void dwarfx32_output(int type, void *param)
1746 int ln, aa, inx, maxln, soc;
1747 struct symlininfo *s;
1748 struct SAA *plinep;
1750 (void)type;
1752 s = (struct symlininfo *)param;
1754 /* line number info is only gathered for executable sections */
1755 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1756 return;
1758 /* Check if section index has changed */
1759 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1760 dwarfx32_findsect(s->section);
1762 /* do nothing unless line or file has changed */
1763 if (!debug_immcall)
1764 return;
1766 ln = currentline - dwarf_csect->line;
1767 aa = s->offset - dwarf_csect->offset;
1768 inx = dwarf_clist->line;
1769 plinep = dwarf_csect->psaa;
1770 /* check for file change */
1771 if (!(inx == dwarf_csect->file)) {
1772 saa_write8(plinep,DW_LNS_set_file);
1773 saa_write8(plinep,inx);
1774 dwarf_csect->file = inx;
1776 /* check for line change */
1777 if (ln) {
1778 /* test if in range of special op code */
1779 maxln = line_base + line_range;
1780 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1781 if (ln >= line_base && ln < maxln && soc < 256) {
1782 saa_write8(plinep,soc);
1783 } else {
1784 saa_write8(plinep,DW_LNS_advance_line);
1785 saa_wleb128s(plinep,ln);
1786 if (aa) {
1787 saa_write8(plinep,DW_LNS_advance_pc);
1788 saa_wleb128u(plinep,aa);
1791 dwarf_csect->line = currentline;
1792 dwarf_csect->offset = s->offset;
1795 /* show change handled */
1796 debug_immcall = 0;
1800 static void dwarfx32_generate(void)
1802 uint8_t *pbuf;
1803 int indx;
1804 struct linelist *ftentry;
1805 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1806 struct SAA *parangesrel, *plinesrel, *pinforel;
1807 struct sectlist *psect;
1808 size_t saalen, linepoff, totlen, highaddr;
1810 /* write epilogues for each line program range */
1811 /* and build aranges section */
1812 paranges = saa_init(1L);
1813 parangesrel = saa_init(1L);
1814 saa_write16(paranges,3); /* dwarf version */
1815 saa_write32(parangesrel, paranges->datalen+4);
1816 saa_write32(parangesrel, (dwarf_infosym << 8) + R_X86_64_32); /* reloc to info */
1817 saa_write32(parangesrel, 0);
1818 saa_write32(paranges,0); /* offset into info */
1819 saa_write8(paranges,4); /* pointer size */
1820 saa_write8(paranges,0); /* not segmented */
1821 saa_write32(paranges,0); /* padding */
1822 /* iterate though sectlist entries */
1823 psect = dwarf_fsect;
1824 totlen = 0;
1825 highaddr = 0;
1826 for (indx = 0; indx < dwarf_nsections; indx++)
1828 plinep = psect->psaa;
1829 /* Line Number Program Epilogue */
1830 saa_write8(plinep,2); /* std op 2 */
1831 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1832 saa_write8(plinep,DW_LNS_extended_op);
1833 saa_write8(plinep,1); /* operand length */
1834 saa_write8(plinep,DW_LNE_end_sequence);
1835 totlen += plinep->datalen;
1836 /* range table relocation entry */
1837 saa_write32(parangesrel, paranges->datalen + 4);
1838 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_X86_64_32);
1839 saa_write32(parangesrel, (uint32_t) 0);
1840 /* range table entry */
1841 saa_write32(paranges,0x0000); /* range start */
1842 saa_write32(paranges,sects[psect->section]->len); /* range length */
1843 highaddr += sects[psect->section]->len;
1844 /* done with this entry */
1845 psect = psect->next;
1847 saa_write32(paranges,0); /* null address */
1848 saa_write32(paranges,0); /* null length */
1849 saalen = paranges->datalen;
1850 arangeslen = saalen + 4;
1851 arangesbuf = pbuf = nasm_malloc(arangeslen);
1852 WRITELONG(pbuf,saalen); /* initial length */
1853 saa_rnbytes(paranges, pbuf, saalen);
1854 saa_free(paranges);
1856 /* build rela.aranges section */
1857 arangesrellen = saalen = parangesrel->datalen;
1858 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1859 saa_rnbytes(parangesrel, pbuf, saalen);
1860 saa_free(parangesrel);
1862 /* build pubnames section */
1863 ppubnames = saa_init(1L);
1864 saa_write16(ppubnames,3); /* dwarf version */
1865 saa_write32(ppubnames,0); /* offset into info */
1866 saa_write32(ppubnames,0); /* space used in info */
1867 saa_write32(ppubnames,0); /* end of list */
1868 saalen = ppubnames->datalen;
1869 pubnameslen = saalen + 4;
1870 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1871 WRITELONG(pbuf,saalen); /* initial length */
1872 saa_rnbytes(ppubnames, pbuf, saalen);
1873 saa_free(ppubnames);
1875 /* build info section */
1876 pinfo = saa_init(1L);
1877 pinforel = saa_init(1L);
1878 saa_write16(pinfo,3); /* dwarf version */
1879 saa_write32(pinforel, pinfo->datalen + 4);
1880 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_X86_64_32); /* reloc to abbrev */
1881 saa_write32(pinforel, 0);
1882 saa_write32(pinfo,0); /* offset into abbrev */
1883 saa_write8(pinfo,4); /* pointer size */
1884 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1885 saa_write32(pinforel, pinfo->datalen + 4);
1886 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
1887 saa_write32(pinforel, 0);
1888 saa_write32(pinfo,0); /* DW_AT_low_pc */
1889 saa_write32(pinforel, pinfo->datalen + 4);
1890 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
1891 saa_write32(pinforel, 0);
1892 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
1893 saa_write32(pinforel, pinfo->datalen + 4);
1894 saa_write32(pinforel, (dwarf_linesym << 8) + R_X86_64_32); /* reloc to line */
1895 saa_write32(pinforel, 0);
1896 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1897 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1898 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1899 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1900 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1901 saa_write32(pinforel, pinfo->datalen + 4);
1902 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
1903 saa_write32(pinforel, 0);
1904 saa_write32(pinfo,0); /* DW_AT_low_pc */
1905 saa_write32(pinfo,0); /* DW_AT_frame_base */
1906 saa_write8(pinfo,0); /* end of entries */
1907 saalen = pinfo->datalen;
1908 infolen = saalen + 4;
1909 infobuf = pbuf = nasm_malloc(infolen);
1910 WRITELONG(pbuf,saalen); /* initial length */
1911 saa_rnbytes(pinfo, pbuf, saalen);
1912 saa_free(pinfo);
1914 /* build rela.info section */
1915 inforellen = saalen = pinforel->datalen;
1916 inforelbuf = pbuf = nasm_malloc(inforellen);
1917 saa_rnbytes(pinforel, pbuf, saalen);
1918 saa_free(pinforel);
1920 /* build abbrev section */
1921 pabbrev = saa_init(1L);
1922 saa_write8(pabbrev,1); /* entry number LEB128u */
1923 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
1924 saa_write8(pabbrev,1); /* has children */
1925 /* the following attributes and forms are all LEB128u values */
1926 saa_write8(pabbrev,DW_AT_low_pc);
1927 saa_write8(pabbrev,DW_FORM_addr);
1928 saa_write8(pabbrev,DW_AT_high_pc);
1929 saa_write8(pabbrev,DW_FORM_addr);
1930 saa_write8(pabbrev,DW_AT_stmt_list);
1931 saa_write8(pabbrev,DW_FORM_data4);
1932 saa_write8(pabbrev,DW_AT_name);
1933 saa_write8(pabbrev,DW_FORM_string);
1934 saa_write8(pabbrev,DW_AT_producer);
1935 saa_write8(pabbrev,DW_FORM_string);
1936 saa_write8(pabbrev,DW_AT_language);
1937 saa_write8(pabbrev,DW_FORM_data2);
1938 saa_write16(pabbrev,0); /* end of entry */
1939 /* LEB128u usage same as above */
1940 saa_write8(pabbrev,2); /* entry number */
1941 saa_write8(pabbrev,DW_TAG_subprogram);
1942 saa_write8(pabbrev,0); /* no children */
1943 saa_write8(pabbrev,DW_AT_low_pc);
1944 saa_write8(pabbrev,DW_FORM_addr);
1945 saa_write8(pabbrev,DW_AT_frame_base);
1946 saa_write8(pabbrev,DW_FORM_data4);
1947 saa_write16(pabbrev,0); /* end of entry */
1948 abbrevlen = saalen = pabbrev->datalen;
1949 abbrevbuf = pbuf = nasm_malloc(saalen);
1950 saa_rnbytes(pabbrev, pbuf, saalen);
1951 saa_free(pabbrev);
1953 /* build line section */
1954 /* prolog */
1955 plines = saa_init(1L);
1956 saa_write8(plines,1); /* Minimum Instruction Length */
1957 saa_write8(plines,1); /* Initial value of 'is_stmt' */
1958 saa_write8(plines,line_base); /* Line Base */
1959 saa_write8(plines,line_range); /* Line Range */
1960 saa_write8(plines,opcode_base); /* Opcode Base */
1961 /* standard opcode lengths (# of LEB128u operands) */
1962 saa_write8(plines,0); /* Std opcode 1 length */
1963 saa_write8(plines,1); /* Std opcode 2 length */
1964 saa_write8(plines,1); /* Std opcode 3 length */
1965 saa_write8(plines,1); /* Std opcode 4 length */
1966 saa_write8(plines,1); /* Std opcode 5 length */
1967 saa_write8(plines,0); /* Std opcode 6 length */
1968 saa_write8(plines,0); /* Std opcode 7 length */
1969 saa_write8(plines,0); /* Std opcode 8 length */
1970 saa_write8(plines,1); /* Std opcode 9 length */
1971 saa_write8(plines,0); /* Std opcode 10 length */
1972 saa_write8(plines,0); /* Std opcode 11 length */
1973 saa_write8(plines,1); /* Std opcode 12 length */
1974 /* Directory Table */
1975 saa_write8(plines,0); /* End of table */
1976 /* File Name Table */
1977 ftentry = dwarf_flist;
1978 for (indx = 0;indx<dwarf_numfiles;indx++)
1980 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
1981 saa_write8(plines,0); /* directory LEB128u */
1982 saa_write8(plines,0); /* time LEB128u */
1983 saa_write8(plines,0); /* size LEB128u */
1984 ftentry = ftentry->next;
1986 saa_write8(plines,0); /* End of table */
1987 linepoff = plines->datalen;
1988 linelen = linepoff + totlen + 10;
1989 linebuf = pbuf = nasm_malloc(linelen);
1990 WRITELONG(pbuf,linelen-4); /* initial length */
1991 WRITESHORT(pbuf,3); /* dwarf version */
1992 WRITELONG(pbuf,linepoff); /* offset to line number program */
1993 /* write line header */
1994 saalen = linepoff;
1995 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
1996 pbuf += linepoff;
1997 saa_free(plines);
1998 /* concatonate line program ranges */
1999 linepoff += 13;
2000 plinesrel = saa_init(1L);
2001 psect = dwarf_fsect;
2002 for (indx = 0; indx < dwarf_nsections; indx++) {
2003 saa_write32(plinesrel, linepoff);
2004 saa_write32(plinesrel, ((psect->section + 2) << 8) + R_X86_64_32);
2005 saa_write32(plinesrel, 0);
2006 plinep = psect->psaa;
2007 saalen = plinep->datalen;
2008 saa_rnbytes(plinep, pbuf, saalen);
2009 pbuf += saalen;
2010 linepoff += saalen;
2011 saa_free(plinep);
2012 /* done with this entry */
2013 psect = psect->next;
2017 /* build rela.lines section */
2018 linerellen =saalen = plinesrel->datalen;
2019 linerelbuf = pbuf = nasm_malloc(linerellen);
2020 saa_rnbytes(plinesrel, pbuf, saalen);
2021 saa_free(plinesrel);
2023 /* build frame section */
2024 framelen = 4;
2025 framebuf = pbuf = nasm_malloc(framelen);
2026 WRITELONG(pbuf,framelen-4); /* initial length */
2028 /* build loc section */
2029 loclen = 16;
2030 locbuf = pbuf = nasm_malloc(loclen);
2031 WRITELONG(pbuf,0); /* null beginning offset */
2032 WRITELONG(pbuf,0); /* null ending offset */
2035 static void dwarfx32_cleanup(void)
2037 nasm_free(arangesbuf);
2038 nasm_free(arangesrelbuf);
2039 nasm_free(pubnamesbuf);
2040 nasm_free(infobuf);
2041 nasm_free(inforelbuf);
2042 nasm_free(abbrevbuf);
2043 nasm_free(linebuf);
2044 nasm_free(linerelbuf);
2045 nasm_free(framebuf);
2046 nasm_free(locbuf);
2049 static void dwarfx32_findfile(const char * fname)
2051 int finx;
2052 struct linelist *match;
2054 /* return if fname is current file name */
2055 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2056 return;
2058 /* search for match */
2059 match = 0;
2060 if (dwarf_flist) {
2061 match = dwarf_flist;
2062 for (finx = 0; finx < dwarf_numfiles; finx++) {
2063 if (!(strcmp(fname, match->filename))) {
2064 dwarf_clist = match;
2065 return;
2070 /* add file name to end of list */
2071 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2072 dwarf_numfiles++;
2073 dwarf_clist->line = dwarf_numfiles;
2074 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2075 strcpy(dwarf_clist->filename,fname);
2076 dwarf_clist->next = 0;
2077 if (!dwarf_flist) { /* if first entry */
2078 dwarf_flist = dwarf_elist = dwarf_clist;
2079 dwarf_clist->last = 0;
2080 } else { /* chain to previous entry */
2081 dwarf_elist->next = dwarf_clist;
2082 dwarf_elist = dwarf_clist;
2086 static void dwarfx32_findsect(const int index)
2088 int sinx;
2089 struct sectlist *match;
2090 struct SAA *plinep;
2092 /* return if index is current section index */
2093 if (dwarf_csect && (dwarf_csect->section == index))
2094 return;
2096 /* search for match */
2097 match = 0;
2098 if (dwarf_fsect) {
2099 match = dwarf_fsect;
2100 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2101 if (match->section == index) {
2102 dwarf_csect = match;
2103 return;
2105 match = match->next;
2109 /* add entry to end of list */
2110 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2111 dwarf_nsections++;
2112 dwarf_csect->psaa = plinep = saa_init(1L);
2113 dwarf_csect->line = 1;
2114 dwarf_csect->offset = 0;
2115 dwarf_csect->file = 1;
2116 dwarf_csect->section = index;
2117 dwarf_csect->next = 0;
2118 /* set relocatable address at start of line program */
2119 saa_write8(plinep,DW_LNS_extended_op);
2120 saa_write8(plinep,5); /* operand length */
2121 saa_write8(plinep,DW_LNE_set_address);
2122 saa_write32(plinep,0); /* Start Address */
2124 if (!dwarf_fsect) { /* if first entry */
2125 dwarf_fsect = dwarf_esect = dwarf_csect;
2126 dwarf_csect->last = 0;
2127 } else { /* chain to previous entry */
2128 dwarf_esect->next = dwarf_csect;
2129 dwarf_esect = dwarf_csect;
2133 #endif /* OF_ELFX32 */