%error, %warning out on the final pass, add %fatal
[nasm/avx512.git] / output / outelf32.c
blobbff264a36f26fb948bce10f0c966bfa7ded2d175
1 /* outelf.c output routines for the Netwide Assembler to produce
2 * ELF32 (i386 of course) object file format
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the license given in the file "LICENSE"
7 * distributed in the NASM archive.
8 */
10 #include "compiler.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <inttypes.h>
18 #include "nasm.h"
19 #include "nasmlib.h"
20 #include "saa.h"
21 #include "raa.h"
22 #include "stdscan.h"
23 #include "outform.h"
25 #ifdef OF_ELF32
28 * Relocation types.
30 enum reloc_type {
31 R_386_32 = 1, /* ordinary absolute relocation */
32 R_386_PC32 = 2, /* PC-relative relocation */
33 R_386_GOT32 = 3, /* an offset into GOT */
34 R_386_PLT32 = 4, /* a PC-relative offset into PLT */
35 R_386_COPY = 5, /* ??? */
36 R_386_GLOB_DAT = 6, /* ??? */
37 R_386_JUMP_SLOT = 7, /* ??? */
38 R_386_RELATIVE = 8, /* ??? */
39 R_386_GOTOFF = 9, /* an offset from GOT base */
40 R_386_GOTPC = 10, /* a PC-relative offset _to_ GOT */
41 /* These are GNU extensions, but useful */
42 R_386_16 = 20, /* A 16-bit absolute relocation */
43 R_386_PC16 = 21, /* A 16-bit PC-relative relocation */
44 R_386_8 = 22, /* An 8-bit absolute relocation */
45 R_386_PC8 = 23 /* An 8-bit PC-relative relocation */
48 struct Reloc {
49 struct Reloc *next;
50 int32_t address; /* relative to _start_ of section */
51 int32_t symbol; /* symbol index */
52 int type; /* type of relocation */
55 struct Symbol {
56 int32_t strpos; /* string table position of name */
57 int32_t section; /* section ID of the symbol */
58 int type; /* symbol type */
59 int other; /* symbol visibility */
60 int32_t value; /* address, or COMMON variable align */
61 int32_t size; /* size of symbol */
62 int32_t globnum; /* symbol table offset if global */
63 struct Symbol *next; /* list of globals in each section */
64 struct Symbol *nextfwd; /* list of unresolved-size symbols */
65 char *name; /* used temporarily if in above list */
68 #define SHT_PROGBITS 1
69 #define SHT_NOBITS 8
71 #define SHF_WRITE 1
72 #define SHF_ALLOC 2
73 #define SHF_EXECINSTR 4
75 struct Section {
76 struct SAA *data;
77 uint32_t len, size, nrelocs;
78 int32_t index;
79 int type; /* SHT_PROGBITS or SHT_NOBITS */
80 int align; /* alignment: power of two */
81 uint32_t flags; /* section flags */
82 char *name;
83 struct SAA *rel;
84 int32_t rellen;
85 struct Reloc *head, **tail;
86 struct Symbol *gsyms; /* global symbols in section */
89 #define SECT_DELTA 32
90 static struct Section **sects;
91 static int nsects, sectlen;
93 #define SHSTR_DELTA 256
94 static char *shstrtab;
95 static int shstrtablen, shstrtabsize;
97 static struct SAA *syms;
98 static uint32_t nlocals, nglobs;
100 static int32_t def_seg;
102 static struct RAA *bsym;
104 static struct SAA *strs;
105 static uint32_t strslen;
107 static FILE *elffp;
108 static efunc error;
109 static evalfunc evaluate;
111 static struct Symbol *fwds;
113 static char elf_module[FILENAME_MAX];
115 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
116 static uint8_t elf_abiver = 0; /* Current ABI version */
118 extern struct ofmt of_elf32;
119 extern struct ofmt of_elf;
121 #define SHN_ABS 0xFFF1
122 #define SHN_COMMON 0xFFF2
123 #define SHN_UNDEF 0
125 #define SYM_GLOBAL 0x10
127 #define SHT_RELA 4 /* Relocation entries with addends */
129 #define STT_NOTYPE 0 /* Symbol type is unspecified */
130 #define STT_OBJECT 1 /* Symbol is a data object */
131 #define STT_FUNC 2 /* Symbol is a code object */
132 #define STT_SECTION 3 /* Symbol associated with a section */
133 #define STT_FILE 4 /* Symbol's name is file name */
134 #define STT_COMMON 5 /* Symbol is a common data object */
135 #define STT_TLS 6 /* Symbol is thread-local data object*/
136 #define STT_NUM 7 /* Number of defined types. */
138 #define STV_DEFAULT 0
139 #define STV_INTERNAL 1
140 #define STV_HIDDEN 2
141 #define STV_PROTECTED 3
143 #define GLOBAL_TEMP_BASE 1048576 /* bigger than any reasonable sym id */
145 #define SEG_ALIGN 16 /* alignment of sections in file */
146 #define SEG_ALIGN_1 (SEG_ALIGN-1)
148 /* Definitions in lieu of dwarf.h */
149 #define DW_TAG_compile_unit 0x11
150 #define DW_TAG_subprogram 0x2e
151 #define DW_AT_name 0x03
152 #define DW_AT_stmt_list 0x10
153 #define DW_AT_low_pc 0x11
154 #define DW_AT_high_pc 0x12
155 #define DW_AT_language 0x13
156 #define DW_AT_producer 0x25
157 #define DW_AT_frame_base 0x40
158 #define DW_FORM_addr 0x01
159 #define DW_FORM_data2 0x05
160 #define DW_FORM_data4 0x06
161 #define DW_FORM_string 0x08
162 #define DW_LNS_extended_op 0
163 #define DW_LNS_advance_pc 2
164 #define DW_LNS_advance_line 3
165 #define DW_LNS_set_file 4
166 #define DW_LNE_end_sequence 1
167 #define DW_LNE_set_address 2
168 #define DW_LNE_define_file 3
169 #define DW_LANG_Mips_Assembler 0x8001
171 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
173 static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
175 static struct ELF_SECTDATA {
176 void *data;
177 int32_t len;
178 bool is_saa;
179 } *elf_sects;
180 static int elf_nsect, nsections;
181 static int32_t elf_foffs;
183 static void elf_write(void);
184 static void elf_sect_write(struct Section *, const uint8_t *,
185 uint32_t);
186 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
187 int, int);
188 static void elf_write_sections(void);
189 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
190 static struct SAA *elf_build_reltab(int32_t *, struct Reloc *);
191 static void add_sectname(char *, char *);
193 /* this stuff is needed for the stabs debugging format */
194 #define N_SO 0x64 /* ID for main source file */
195 #define N_SOL 0x84 /* ID for sub-source file */
196 #define N_BINCL 0x82
197 #define N_EINCL 0xA2
198 #define N_SLINE 0x44
199 #define TY_STABSSYMLIN 0x40 /* ouch */
201 struct stabentry {
202 uint32_t n_strx;
203 uint8_t n_type;
204 uint8_t n_other;
205 uint16_t n_desc;
206 uint32_t n_value;
209 struct erel {
210 int offset, info;
213 struct symlininfo {
214 int offset;
215 int section; /* section index */
216 char *name; /* shallow-copied pointer of section name */
219 struct linelist {
220 struct symlininfo info;
221 int line;
222 char *filename;
223 struct linelist *next;
224 struct linelist *last;
227 struct sectlist {
228 struct SAA *psaa;
229 int section;
230 int line;
231 int offset;
232 int file;
233 struct sectlist *next;
234 struct sectlist *last;
237 /* common debug variables */
238 static int currentline = 1;
239 static int debug_immcall = 0;
241 /* stabs debug variables */
242 static struct linelist *stabslines = 0;
243 static int numlinestabs = 0;
244 static char *stabs_filename = 0;
245 static int symtabsection;
246 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
247 static int stablen, stabstrlen, stabrellen;
249 /* dwarf debug variables */
250 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
251 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
252 static int dwarf_numfiles = 0, dwarf_nsections;
253 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
254 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
255 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
256 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
257 abbrevlen, linelen, linerellen, framelen, loclen;
258 static int32_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
260 static struct dfmt df_dwarf;
261 static struct dfmt df_stabs;
262 static struct Symbol *lastsym;
264 /* common debugging routines */
265 void debug32_typevalue(int32_t);
266 void debug32_init(struct ofmt *, void *, FILE *, efunc);
267 void debug32_deflabel(char *, int32_t, int64_t, int, char *);
268 void debug32_directive(const char *, const char *);
270 /* stabs debugging routines */
271 void stabs32_linenum(const char *filename, int32_t linenumber, int32_t);
272 void stabs32_output(int, void *);
273 void stabs32_generate(void);
274 void stabs32_cleanup(void);
276 /* dwarf debugging routines */
277 void dwarf32_linenum(const char *filename, int32_t linenumber, int32_t);
278 void dwarf32_output(int, void *);
279 void dwarf32_generate(void);
280 void dwarf32_cleanup(void);
281 void dwarf32_findfile(const char *);
282 void dwarf32_findsect(const int);
283 void saa_wleb128u(struct SAA *, int);
284 void saa_wleb128s(struct SAA *, int);
287 * Special section numbers which are used to define ELF special
288 * symbols, which can be used with WRT to provide PIC relocation
289 * types.
291 static int32_t elf_gotpc_sect, elf_gotoff_sect;
292 static int32_t elf_got_sect, elf_plt_sect;
293 static int32_t elf_sym_sect;
295 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
297 if (of_elf.current_dfmt != &null_debug_form)
298 of_elf32.current_dfmt = of_elf.current_dfmt;
299 elffp = fp;
300 error = errfunc;
301 evaluate = eval;
302 (void)ldef; /* placate optimisers */
303 sects = NULL;
304 nsects = sectlen = 0;
305 syms = saa_init((int32_t)sizeof(struct Symbol));
306 nlocals = nglobs = 0;
307 bsym = raa_init();
308 strs = saa_init(1L);
309 saa_wbytes(strs, "\0", 1L);
310 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
311 strslen = 2 + strlen(elf_module);
312 shstrtab = NULL;
313 shstrtablen = shstrtabsize = 0;;
314 add_sectname("", "");
316 fwds = NULL;
318 elf_gotpc_sect = seg_alloc();
319 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf32,
320 error);
321 elf_gotoff_sect = seg_alloc();
322 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf32,
323 error);
324 elf_got_sect = seg_alloc();
325 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf32,
326 error);
327 elf_plt_sect = seg_alloc();
328 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf32,
329 error);
330 elf_sym_sect = seg_alloc();
331 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf32,
332 error);
334 def_seg = seg_alloc();
337 static void elf_cleanup(int debuginfo)
339 struct Reloc *r;
340 int i;
342 (void)debuginfo;
344 elf_write();
345 fclose(elffp);
346 for (i = 0; i < nsects; i++) {
347 if (sects[i]->type != SHT_NOBITS)
348 saa_free(sects[i]->data);
349 if (sects[i]->head)
350 saa_free(sects[i]->rel);
351 while (sects[i]->head) {
352 r = sects[i]->head;
353 sects[i]->head = sects[i]->head->next;
354 nasm_free(r);
357 nasm_free(sects);
358 saa_free(syms);
359 raa_free(bsym);
360 saa_free(strs);
361 if (of_elf32.current_dfmt) {
362 of_elf32.current_dfmt->cleanup();
366 static void add_sectname(char *firsthalf, char *secondhalf)
368 int len = strlen(firsthalf) + strlen(secondhalf);
369 while (shstrtablen + len + 1 > shstrtabsize)
370 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
371 strcpy(shstrtab + shstrtablen, firsthalf);
372 strcat(shstrtab + shstrtablen, secondhalf);
373 shstrtablen += len + 1;
376 static int elf_make_section(char *name, int type, int flags, int align)
378 struct Section *s;
380 s = nasm_malloc(sizeof(*s));
382 if (type != SHT_NOBITS)
383 s->data = saa_init(1L);
384 s->head = NULL;
385 s->tail = &s->head;
386 s->len = s->size = 0;
387 s->nrelocs = 0;
388 if (!strcmp(name, ".text"))
389 s->index = def_seg;
390 else
391 s->index = seg_alloc();
392 add_sectname("", name);
393 s->name = nasm_malloc(1 + strlen(name));
394 strcpy(s->name, name);
395 s->type = type;
396 s->flags = flags;
397 s->align = align;
398 s->gsyms = NULL;
400 if (nsects >= sectlen)
401 sects =
402 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
403 sects[nsects++] = s;
405 return nsects - 1;
408 static int32_t elf_section_names(char *name, int pass, int *bits)
410 char *p;
411 unsigned flags_and, flags_or;
412 int type, align, i;
415 * Default is 32 bits.
417 if (!name) {
418 *bits = 32;
419 return def_seg;
422 p = name;
423 while (*p && !nasm_isspace(*p))
424 p++;
425 if (*p)
426 *p++ = '\0';
427 flags_and = flags_or = type = align = 0;
429 while (*p && nasm_isspace(*p))
430 p++;
431 while (*p) {
432 char *q = p;
433 while (*p && !nasm_isspace(*p))
434 p++;
435 if (*p)
436 *p++ = '\0';
437 while (*p && nasm_isspace(*p))
438 p++;
440 if (!nasm_strnicmp(q, "align=", 6)) {
441 align = atoi(q + 6);
442 if (align == 0)
443 align = 1;
444 if ((align - 1) & align) { /* means it's not a power of two */
445 error(ERR_NONFATAL, "section alignment %d is not"
446 " a power of two", align);
447 align = 1;
449 } else if (!nasm_stricmp(q, "alloc")) {
450 flags_and |= SHF_ALLOC;
451 flags_or |= SHF_ALLOC;
452 } else if (!nasm_stricmp(q, "noalloc")) {
453 flags_and |= SHF_ALLOC;
454 flags_or &= ~SHF_ALLOC;
455 } else if (!nasm_stricmp(q, "exec")) {
456 flags_and |= SHF_EXECINSTR;
457 flags_or |= SHF_EXECINSTR;
458 } else if (!nasm_stricmp(q, "noexec")) {
459 flags_and |= SHF_EXECINSTR;
460 flags_or &= ~SHF_EXECINSTR;
461 } else if (!nasm_stricmp(q, "write")) {
462 flags_and |= SHF_WRITE;
463 flags_or |= SHF_WRITE;
464 } else if (!nasm_stricmp(q, "nowrite")) {
465 flags_and |= SHF_WRITE;
466 flags_or &= ~SHF_WRITE;
467 } else if (!nasm_stricmp(q, "progbits")) {
468 type = SHT_PROGBITS;
469 } else if (!nasm_stricmp(q, "nobits")) {
470 type = SHT_NOBITS;
474 if (!strcmp(name, ".comment") ||
475 !strcmp(name, ".shstrtab") ||
476 !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
477 error(ERR_NONFATAL, "attempt to redefine reserved section"
478 "name `%s'", name);
479 return NO_SEG;
482 for (i = 0; i < nsects; i++)
483 if (!strcmp(name, sects[i]->name))
484 break;
485 if (i == nsects) {
486 if (!strcmp(name, ".text"))
487 i = elf_make_section(name, SHT_PROGBITS,
488 SHF_ALLOC | SHF_EXECINSTR, 16);
489 else if (!strcmp(name, ".rodata"))
490 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
491 else if (!strcmp(name, ".data"))
492 i = elf_make_section(name, SHT_PROGBITS,
493 SHF_ALLOC | SHF_WRITE, 4);
494 else if (!strcmp(name, ".bss"))
495 i = elf_make_section(name, SHT_NOBITS,
496 SHF_ALLOC | SHF_WRITE, 4);
497 else
498 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
499 if (type)
500 sects[i]->type = type;
501 if (align)
502 sects[i]->align = align;
503 sects[i]->flags &= ~flags_and;
504 sects[i]->flags |= flags_or;
505 } else if (pass == 1) {
506 if ((type && sects[i]->type != type)
507 || (align && sects[i]->align != align)
508 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
509 error(ERR_WARNING, "section attributes ignored on"
510 " redeclaration of section `%s'", name);
513 return sects[i]->index;
516 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
517 int is_global, char *special)
519 int pos = strslen;
520 struct Symbol *sym;
521 bool special_used = false;
523 #if defined(DEBUG) && DEBUG>2
524 fprintf(stderr,
525 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
526 name, segment, offset, is_global, special);
527 #endif
528 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
530 * This is a NASM special symbol. We never allow it into
531 * the ELF symbol table, even if it's a valid one. If it
532 * _isn't_ a valid one, we should barf immediately.
534 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
535 strcmp(name, "..got") && strcmp(name, "..plt") &&
536 strcmp(name, "..sym"))
537 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
538 return;
541 if (is_global == 3) {
542 struct Symbol **s;
544 * Fix up a forward-reference symbol size from the first
545 * pass.
547 for (s = &fwds; *s; s = &(*s)->nextfwd)
548 if (!strcmp((*s)->name, name)) {
549 struct tokenval tokval;
550 expr *e;
551 char *p = special;
553 while (*p && !nasm_isspace(*p))
554 p++;
555 while (*p && nasm_isspace(*p))
556 p++;
557 stdscan_reset();
558 stdscan_bufptr = p;
559 tokval.t_type = TOKEN_INVALID;
560 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
561 if (e) {
562 if (!is_simple(e))
563 error(ERR_NONFATAL, "cannot use relocatable"
564 " expression as symbol size");
565 else
566 (*s)->size = reloc_value(e);
570 * Remove it from the list of unresolved sizes.
572 nasm_free((*s)->name);
573 *s = (*s)->nextfwd;
574 return;
576 return; /* it wasn't an important one */
579 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
580 strslen += 1 + strlen(name);
582 lastsym = sym = saa_wstruct(syms);
584 sym->strpos = pos;
585 sym->type = is_global ? SYM_GLOBAL : 0;
586 sym->other = STV_DEFAULT;
587 sym->size = 0;
588 if (segment == NO_SEG)
589 sym->section = SHN_ABS;
590 else {
591 int i;
592 sym->section = SHN_UNDEF;
593 if (nsects == 0 && segment == def_seg) {
594 int tempint;
595 if (segment != elf_section_names(".text", 2, &tempint))
596 error(ERR_PANIC,
597 "strange segment conditions in ELF driver");
598 sym->section = nsects;
599 } else {
600 for (i = 0; i < nsects; i++)
601 if (segment == sects[i]->index) {
602 sym->section = i + 1;
603 break;
608 if (is_global == 2) {
609 sym->size = offset;
610 sym->value = 0;
611 sym->section = SHN_COMMON;
613 * We have a common variable. Check the special text to see
614 * if it's a valid number and power of two; if so, store it
615 * as the alignment for the common variable.
617 if (special) {
618 bool err;
619 sym->value = readnum(special, &err);
620 if (err)
621 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
622 " valid number", special);
623 else if ((sym->value | (sym->value - 1)) != 2 * sym->value - 1)
624 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
625 " power of two", special);
627 special_used = true;
628 } else
629 sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
631 if (sym->type == SYM_GLOBAL) {
633 * If sym->section == SHN_ABS, then the first line of the
634 * else section would cause a core dump, because its a reference
635 * beyond the end of the section array.
636 * This behaviour is exhibited by this code:
637 * GLOBAL crash_nasm
638 * crash_nasm equ 0
639 * To avoid such a crash, such requests are silently discarded.
640 * This may not be the best solution.
642 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
643 bsym = raa_write(bsym, segment, nglobs);
644 } else if (sym->section != SHN_ABS) {
646 * This is a global symbol; so we must add it to the linked
647 * list of global symbols in its section. We'll push it on
648 * the beginning of the list, because it doesn't matter
649 * much which end we put it on and it's easier like this.
651 * In addition, we check the special text for symbol
652 * type and size information.
654 sym->next = sects[sym->section - 1]->gsyms;
655 sects[sym->section - 1]->gsyms = sym;
657 if (special) {
658 int n = strcspn(special, " \t");
660 if (!nasm_strnicmp(special, "function", n))
661 sym->type |= STT_FUNC;
662 else if (!nasm_strnicmp(special, "data", n) ||
663 !nasm_strnicmp(special, "object", n))
664 sym->type |= STT_OBJECT;
665 else if (!nasm_strnicmp(special, "notype", n))
666 sym->type |= STT_NOTYPE;
667 else
668 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
669 n, special);
670 special += n;
672 while (nasm_isspace(*special))
673 ++special;
674 if (*special) {
675 n = strcspn(special, " \t");
676 if (!nasm_strnicmp(special, "default", n))
677 sym->other = STV_DEFAULT;
678 else if (!nasm_strnicmp(special, "internal", n))
679 sym->other = STV_INTERNAL;
680 else if (!nasm_strnicmp(special, "hidden", n))
681 sym->other = STV_HIDDEN;
682 else if (!nasm_strnicmp(special, "protected", n))
683 sym->other = STV_PROTECTED;
684 else
685 n = 0;
686 special += n;
689 if (*special) {
690 struct tokenval tokval;
691 expr *e;
692 int fwd = 0;
693 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
695 while (special[n] && nasm_isspace(special[n]))
696 n++;
698 * We have a size expression; attempt to
699 * evaluate it.
701 stdscan_reset();
702 stdscan_bufptr = special + n;
703 tokval.t_type = TOKEN_INVALID;
704 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
705 NULL);
706 if (fwd) {
707 sym->nextfwd = fwds;
708 fwds = sym;
709 sym->name = nasm_strdup(name);
710 } else if (e) {
711 if (!is_simple(e))
712 error(ERR_NONFATAL, "cannot use relocatable"
713 " expression as symbol size");
714 else
715 sym->size = reloc_value(e);
717 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
719 special_used = true;
722 sym->globnum = nglobs;
723 nglobs++;
724 } else
725 nlocals++;
727 if (special && !special_used)
728 error(ERR_NONFATAL, "no special symbol features supported here");
731 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
733 struct Reloc *r;
735 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
736 sect->tail = &r->next;
737 r->next = NULL;
739 r->address = sect->len;
740 if (segment == NO_SEG)
741 r->symbol = 0;
742 else {
743 int i;
744 r->symbol = 0;
745 for (i = 0; i < nsects; i++)
746 if (segment == sects[i]->index)
747 r->symbol = i + 2;
748 if (!r->symbol)
749 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
751 r->type = type;
753 sect->nrelocs++;
757 * This routine deals with ..got and ..sym relocations: the more
758 * complicated kinds. In shared-library writing, some relocations
759 * with respect to global symbols must refer to the precise symbol
760 * rather than referring to an offset from the base of the section
761 * _containing_ the symbol. Such relocations call to this routine,
762 * which searches the symbol list for the symbol in question.
764 * R_386_GOT32 references require the _exact_ symbol address to be
765 * used; R_386_32 references can be at an offset from the symbol.
766 * The boolean argument `exact' tells us this.
768 * Return value is the adjusted value of `addr', having become an
769 * offset from the symbol rather than the section. Should always be
770 * zero when returning from an exact call.
772 * Limitation: if you define two symbols at the same place,
773 * confusion will occur.
775 * Inefficiency: we search, currently, using a linked list which
776 * isn't even necessarily sorted.
778 static int32_t elf_add_gsym_reloc(struct Section *sect,
779 int32_t segment, int32_t offset,
780 int type, bool exact)
782 struct Reloc *r;
783 struct Section *s;
784 struct Symbol *sym, *sm;
785 int i;
788 * First look up the segment/offset pair and find a global
789 * symbol corresponding to it. If it's not one of our segments,
790 * then it must be an external symbol, in which case we're fine
791 * doing a normal elf_add_reloc after first sanity-checking
792 * that the offset from the symbol is zero.
794 s = NULL;
795 for (i = 0; i < nsects; i++)
796 if (segment == sects[i]->index) {
797 s = sects[i];
798 break;
800 if (!s) {
801 if (exact && offset != 0)
802 error(ERR_NONFATAL, "unable to find a suitable global symbol"
803 " for this reference");
804 else
805 elf_add_reloc(sect, segment, type);
806 return offset;
809 if (exact) {
811 * Find a symbol pointing _exactly_ at this one.
813 for (sym = s->gsyms; sym; sym = sym->next)
814 if (sym->value == offset)
815 break;
816 } else {
818 * Find the nearest symbol below this one.
820 sym = NULL;
821 for (sm = s->gsyms; sm; sm = sm->next)
822 if (sm->value <= offset && (!sym || sm->value > sym->value))
823 sym = sm;
825 if (!sym && exact) {
826 error(ERR_NONFATAL, "unable to find a suitable global symbol"
827 " for this reference");
828 return 0;
831 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
832 sect->tail = &r->next;
833 r->next = NULL;
835 r->address = sect->len;
836 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
837 r->type = type;
839 sect->nrelocs++;
841 return offset - sym->value;
844 static void elf_out(int32_t segto, const void *data,
845 enum out_type type, uint64_t size,
846 int32_t segment, int32_t wrt)
848 struct Section *s;
849 int32_t addr;
850 uint8_t mydata[4], *p;
851 int i;
852 static struct symlininfo sinfo;
855 * handle absolute-assembly (structure definitions)
857 if (segto == NO_SEG) {
858 if (type != OUT_RESERVE)
859 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
860 " space");
861 return;
864 s = NULL;
865 for (i = 0; i < nsects; i++)
866 if (segto == sects[i]->index) {
867 s = sects[i];
868 break;
870 if (!s) {
871 int tempint; /* ignored */
872 if (segto != elf_section_names(".text", 2, &tempint))
873 error(ERR_PANIC, "strange segment conditions in ELF driver");
874 else {
875 s = sects[nsects - 1];
876 i = nsects - 1;
880 /* again some stabs debugging stuff */
881 if (of_elf32.current_dfmt) {
882 sinfo.offset = s->len;
883 sinfo.section = i;
884 sinfo.name = s->name;
885 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
887 /* end of debugging stuff */
889 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
890 error(ERR_WARNING, "attempt to initialize memory in"
891 " BSS section `%s': ignored", s->name);
892 if (type == OUT_REL2ADR)
893 size = 2;
894 else if (type == OUT_REL4ADR)
895 size = 4;
896 s->len += size;
897 return;
900 if (type == OUT_RESERVE) {
901 if (s->type == SHT_PROGBITS) {
902 error(ERR_WARNING, "uninitialized space declared in"
903 " non-BSS section `%s': zeroing", s->name);
904 elf_sect_write(s, NULL, size);
905 } else
906 s->len += size;
907 } else if (type == OUT_RAWDATA) {
908 if (segment != NO_SEG)
909 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
910 elf_sect_write(s, data, size);
911 } else if (type == OUT_ADDRESS) {
912 bool gnu16 = false;
913 addr = *(int64_t *)data;
914 if (segment != NO_SEG) {
915 if (segment % 2) {
916 error(ERR_NONFATAL, "ELF format does not support"
917 " segment base references");
918 } else {
919 if (wrt == NO_SEG) {
920 if (size == 2) {
921 gnu16 = true;
922 elf_add_reloc(s, segment, R_386_16);
923 } else {
924 elf_add_reloc(s, segment, R_386_32);
926 } else if (wrt == elf_gotpc_sect + 1) {
928 * The user will supply GOT relative to $$. ELF
929 * will let us have GOT relative to $. So we
930 * need to fix up the data item by $-$$.
932 addr += s->len;
933 elf_add_reloc(s, segment, R_386_GOTPC);
934 } else if (wrt == elf_gotoff_sect + 1) {
935 elf_add_reloc(s, segment, R_386_GOTOFF);
936 } else if (wrt == elf_got_sect + 1) {
937 addr = elf_add_gsym_reloc(s, segment, addr,
938 R_386_GOT32, true);
939 } else if (wrt == elf_sym_sect + 1) {
940 if (size == 2) {
941 gnu16 = true;
942 addr = elf_add_gsym_reloc(s, segment, addr,
943 R_386_16, false);
944 } else {
945 addr = elf_add_gsym_reloc(s, segment, addr,
946 R_386_32, false);
948 } else if (wrt == elf_plt_sect + 1) {
949 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
950 "relative PLT references");
951 } else {
952 error(ERR_NONFATAL, "ELF format does not support this"
953 " use of WRT");
954 wrt = NO_SEG; /* we can at least _try_ to continue */
958 p = mydata;
959 if (gnu16) {
960 error(ERR_WARNING | ERR_WARN_GNUELF,
961 "16-bit relocations in ELF is a GNU extension");
962 WRITESHORT(p, addr);
963 } else {
964 if (size != 4 && segment != NO_SEG) {
965 error(ERR_NONFATAL,
966 "Unsupported non-32-bit ELF relocation");
968 WRITELONG(p, addr);
970 elf_sect_write(s, mydata, size);
971 } else if (type == OUT_REL2ADR) {
972 if (segment == segto)
973 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
974 if (segment != NO_SEG && segment % 2) {
975 error(ERR_NONFATAL, "ELF format does not support"
976 " segment base references");
977 } else {
978 if (wrt == NO_SEG) {
979 error(ERR_WARNING | ERR_WARN_GNUELF,
980 "16-bit relocations in ELF is a GNU extension");
981 elf_add_reloc(s, segment, R_386_PC16);
982 } else {
983 error(ERR_NONFATAL,
984 "Unsupported non-32-bit ELF relocation");
987 p = mydata;
988 WRITESHORT(p, *(int64_t *)data - size);
989 elf_sect_write(s, mydata, 2L);
990 } else if (type == OUT_REL4ADR) {
991 if (segment == segto)
992 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
993 if (segment != NO_SEG && segment % 2) {
994 error(ERR_NONFATAL, "ELF format does not support"
995 " segment base references");
996 } else {
997 if (wrt == NO_SEG) {
998 elf_add_reloc(s, segment, R_386_PC32);
999 } else if (wrt == elf_plt_sect + 1) {
1000 elf_add_reloc(s, segment, R_386_PLT32);
1001 } else if (wrt == elf_gotpc_sect + 1 ||
1002 wrt == elf_gotoff_sect + 1 ||
1003 wrt == elf_got_sect + 1) {
1004 error(ERR_NONFATAL, "ELF format cannot produce PC-"
1005 "relative GOT references");
1006 } else {
1007 error(ERR_NONFATAL, "ELF format does not support this"
1008 " use of WRT");
1009 wrt = NO_SEG; /* we can at least _try_ to continue */
1012 p = mydata;
1013 WRITELONG(p, *(int64_t *)data - size);
1014 elf_sect_write(s, mydata, 4L);
1018 static void elf_write(void)
1020 int align;
1021 int scount;
1022 char *p;
1023 int commlen;
1024 char comment[64];
1025 int i;
1027 struct SAA *symtab;
1028 int32_t symtablen, symtablocal;
1031 * Work out how many sections we will have. We have SHN_UNDEF,
1032 * then the flexible user sections, then the four fixed
1033 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
1034 * then optionally relocation sections for the user sections.
1036 if (of_elf32.current_dfmt == &df_stabs)
1037 nsections = 8;
1038 else if (of_elf32.current_dfmt == &df_dwarf)
1039 nsections = 15;
1040 else
1041 nsections = 5; /* SHN_UNDEF and the fixed ones */
1043 add_sectname("", ".comment");
1044 add_sectname("", ".shstrtab");
1045 add_sectname("", ".symtab");
1046 add_sectname("", ".strtab");
1047 for (i = 0; i < nsects; i++) {
1048 nsections++; /* for the section itself */
1049 if (sects[i]->head) {
1050 nsections++; /* for its relocations */
1051 add_sectname(".rel", sects[i]->name);
1055 if (of_elf32.current_dfmt == &df_stabs) {
1056 /* in case the debug information is wanted, just add these three sections... */
1057 add_sectname("", ".stab");
1058 add_sectname("", ".stabstr");
1059 add_sectname(".rel", ".stab");
1062 else if (of_elf32.current_dfmt == &df_dwarf) {
1063 /* the dwarf debug standard specifies the following ten sections,
1064 not all of which are currently implemented,
1065 although all of them are defined. */
1066 #define debug_aranges (int32_t) (nsections-10)
1067 #define debug_info (int32_t) (nsections-7)
1068 #define debug_abbrev (int32_t) (nsections-5)
1069 #define debug_line (int32_t) (nsections-4)
1070 add_sectname("", ".debug_aranges");
1071 add_sectname(".rela", ".debug_aranges");
1072 add_sectname("", ".debug_pubnames");
1073 add_sectname("", ".debug_info");
1074 add_sectname(".rela", ".debug_info");
1075 add_sectname("", ".debug_abbrev");
1076 add_sectname("", ".debug_line");
1077 add_sectname(".rela", ".debug_line");
1078 add_sectname("", ".debug_frame");
1079 add_sectname("", ".debug_loc");
1083 * Do the comment.
1085 *comment = '\0';
1086 commlen =
1087 2 + sprintf(comment + 1, "The Netwide Assembler %s", NASM_VER);
1090 * Output the ELF header.
1092 fwrite("\177ELF\1\1\1", 7, 1, elffp);
1093 fputc(elf_osabi, elffp);
1094 fputc(elf_abiver, elffp);
1095 fwrite("\0\0\0\0\0\0\0", 7, 1, elffp);
1096 fwriteint16_t(1, elffp); /* ET_REL relocatable file */
1097 fwriteint16_t(3, elffp); /* EM_386 processor ID */
1098 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1099 fwriteint32_t(0L, elffp); /* no entry point */
1100 fwriteint32_t(0L, elffp); /* no program header table */
1101 fwriteint32_t(0x40L, elffp); /* section headers straight after
1102 * ELF header plus alignment */
1103 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1104 fwriteint16_t(0x34, elffp); /* size of ELF header */
1105 fwriteint16_t(0, elffp); /* no program header table, again */
1106 fwriteint16_t(0, elffp); /* still no program header table */
1107 fwriteint16_t(0x28, elffp); /* size of section header */
1108 fwriteint16_t(nsections, elffp); /* number of sections */
1109 fwriteint16_t(nsects + 2, elffp); /* string table section index for
1110 * section header table */
1111 fwriteint32_t(0L, elffp); /* align to 0x40 bytes */
1112 fwriteint32_t(0L, elffp);
1113 fwriteint32_t(0L, elffp);
1116 * Build the symbol table and relocation tables.
1118 symtab = elf_build_symtab(&symtablen, &symtablocal);
1119 for (i = 0; i < nsects; i++)
1120 if (sects[i]->head)
1121 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1122 sects[i]->head);
1125 * Now output the section header table.
1128 elf_foffs = 0x40 + 0x28 * nsections;
1129 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1130 elf_foffs += align;
1131 elf_nsect = 0;
1132 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1134 elf_section_header(0, 0, 0, NULL, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1135 scount = 1; /* needed for the stabs debugging to track the symtable section */
1136 p = shstrtab + 1;
1137 for (i = 0; i < nsects; i++) {
1138 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1139 (sects[i]->type == SHT_PROGBITS ?
1140 sects[i]->data : NULL), true,
1141 sects[i]->len, 0, 0, sects[i]->align, 0);
1142 p += strlen(p) + 1;
1143 scount++; /* dito */
1145 elf_section_header(p - shstrtab, 1, 0, comment, false, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
1146 scount++; /* dito */
1147 p += strlen(p) + 1;
1148 elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1149 scount++; /* dito */
1150 p += strlen(p) + 1;
1151 elf_section_header(p - shstrtab, 2, 0, symtab, true, symtablen, nsects + 4, symtablocal, 4, 16); /* .symtab */
1152 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1153 p += strlen(p) + 1;
1154 elf_section_header(p - shstrtab, 3, 0, strs, true, strslen, 0, 0, 1, 0); /* .strtab */
1155 for (i = 0; i < nsects; i++)
1156 if (sects[i]->head) {
1157 p += strlen(p) + 1;
1158 elf_section_header(p - shstrtab, 9, 0, sects[i]->rel, true,
1159 sects[i]->rellen, nsects + 3, i + 1, 4, 8);
1161 if (of_elf32.current_dfmt == &df_stabs) {
1162 /* for debugging information, create the last three sections
1163 which are the .stab , .stabstr and .rel.stab sections respectively */
1165 /* this function call creates the stab sections in memory */
1166 stabs32_generate();
1168 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1169 p += strlen(p) + 1;
1170 elf_section_header(p - shstrtab, 1, 0, stabbuf, false, stablen,
1171 nsections - 2, 0, 4, 12);
1173 p += strlen(p) + 1;
1174 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, false,
1175 stabstrlen, 0, 0, 4, 0);
1177 p += strlen(p) + 1;
1178 /* link -> symtable info -> section to refer to */
1179 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, false,
1180 stabrellen, symtabsection, nsections - 3, 4,
1184 else if (of_elf32.current_dfmt == &df_dwarf) {
1185 /* for dwarf debugging information, create the ten dwarf sections */
1187 /* this function call creates the dwarf sections in memory */
1188 if (dwarf_fsect) dwarf32_generate();
1190 p += strlen(p) + 1;
1191 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1192 arangeslen, 0, 0, 1, 0);
1193 p += strlen(p) + 1;
1194 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1195 arangesrellen, symtabsection, debug_aranges, 1, 12);
1196 p += strlen(p) + 1;
1197 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1198 pubnameslen, 0, 0, 1, 0);
1199 p += strlen(p) + 1;
1200 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1201 infolen, 0, 0, 1, 0);
1202 p += strlen(p) + 1;
1203 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1204 inforellen, symtabsection, debug_info, 1, 12);
1205 p += strlen(p) + 1;
1206 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1207 abbrevlen, 0, 0, 1, 0);
1208 p += strlen(p) + 1;
1209 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1210 linelen, 0, 0, 1, 0);
1211 p += strlen(p) + 1;
1212 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1213 linerellen, symtabsection, debug_line, 1, 12);
1214 p += strlen(p) + 1;
1215 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1216 framelen, 0, 0, 8, 0);
1217 p += strlen(p) + 1;
1218 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1219 loclen, 0, 0, 1, 0);
1222 fwrite(align_str, align, 1, elffp);
1225 * Now output the sections.
1227 elf_write_sections();
1229 nasm_free(elf_sects);
1230 saa_free(symtab);
1233 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1235 struct SAA *s = saa_init(1L);
1236 struct Symbol *sym;
1237 uint8_t entry[16], *p;
1238 int i;
1240 *len = *local = 0;
1243 * First, an all-zeros entry, required by the ELF spec.
1245 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1246 *len += 16;
1247 (*local)++;
1250 * Next, an entry for the file name.
1252 p = entry;
1253 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1254 WRITELONG(p, 0); /* no value */
1255 WRITELONG(p, 0); /* no size either */
1256 WRITESHORT(p, STT_FILE); /* type FILE */
1257 WRITESHORT(p, SHN_ABS);
1258 saa_wbytes(s, entry, 16L);
1259 *len += 16;
1260 (*local)++;
1263 * Now some standard symbols defining the segments, for relocation
1264 * purposes.
1266 for (i = 1; i <= nsects; i++) {
1267 p = entry;
1268 WRITELONG(p, 0); /* no symbol name */
1269 WRITELONG(p, 0); /* offset zero */
1270 WRITELONG(p, 0); /* size zero */
1271 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1272 WRITESHORT(p, i); /* section id */
1273 saa_wbytes(s, entry, 16L);
1274 *len += 16;
1275 (*local)++;
1279 * Now the other local symbols.
1281 saa_rewind(syms);
1282 while ((sym = saa_rstruct(syms))) {
1283 if (sym->type & SYM_GLOBAL)
1284 continue;
1285 p = entry;
1286 WRITELONG(p, sym->strpos);
1287 WRITELONG(p, sym->value);
1288 WRITELONG(p, sym->size);
1289 WRITECHAR(p, sym->type); /* type and binding */
1290 WRITECHAR(p, sym->other); /* visibility */
1291 WRITESHORT(p, sym->section);
1292 saa_wbytes(s, entry, 16L);
1293 *len += 16;
1294 (*local)++;
1297 * dwarf needs symbols for debug sections
1298 * which are relocation targets.
1300 //*** fix for 32 bit
1301 if (of_elf32.current_dfmt == &df_dwarf) {
1302 dwarf_infosym = *local;
1303 p = entry;
1304 WRITELONG(p, 0); /* no symbol name */
1305 WRITELONG(p, (uint32_t) 0); /* offset zero */
1306 WRITELONG(p, (uint32_t) 0); /* size zero */
1307 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1308 WRITESHORT(p, debug_info); /* section id */
1309 saa_wbytes(s, entry, 16L);
1310 *len += 16;
1311 (*local)++;
1312 dwarf_abbrevsym = *local;
1313 p = entry;
1314 WRITELONG(p, 0); /* no symbol name */
1315 WRITELONG(p, (uint32_t) 0); /* offset zero */
1316 WRITELONG(p, (uint32_t) 0); /* size zero */
1317 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1318 WRITESHORT(p, debug_abbrev); /* section id */
1319 saa_wbytes(s, entry, 16L);
1320 *len += 16;
1321 (*local)++;
1322 dwarf_linesym = *local;
1323 p = entry;
1324 WRITELONG(p, 0); /* no symbol name */
1325 WRITELONG(p, (uint32_t) 0); /* offset zero */
1326 WRITELONG(p, (uint32_t) 0); /* size zero */
1327 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1328 WRITESHORT(p, debug_line); /* section id */
1329 saa_wbytes(s, entry, 16L);
1330 *len += 16;
1331 (*local)++;
1335 * Now the global symbols.
1337 saa_rewind(syms);
1338 while ((sym = saa_rstruct(syms))) {
1339 if (!(sym->type & SYM_GLOBAL))
1340 continue;
1341 p = entry;
1342 WRITELONG(p, sym->strpos);
1343 WRITELONG(p, sym->value);
1344 WRITELONG(p, sym->size);
1345 WRITECHAR(p, sym->type); /* type and binding */
1346 WRITECHAR(p, sym->other); /* visibility */
1347 WRITESHORT(p, sym->section);
1348 saa_wbytes(s, entry, 16L);
1349 *len += 16;
1352 return s;
1355 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1357 struct SAA *s;
1358 uint8_t *p, entry[8];
1360 if (!r)
1361 return NULL;
1363 s = saa_init(1L);
1364 *len = 0;
1366 while (r) {
1367 int32_t sym = r->symbol;
1369 if (sym >= GLOBAL_TEMP_BASE)
1371 if (of_elf32.current_dfmt == &df_dwarf)
1372 sym += -GLOBAL_TEMP_BASE + (nsects + 5) + nlocals;
1373 else sym += -GLOBAL_TEMP_BASE + (nsects + 2) + nlocals;
1376 p = entry;
1377 WRITELONG(p, r->address);
1378 WRITELONG(p, (sym << 8) + r->type);
1379 saa_wbytes(s, entry, 8L);
1380 *len += 8;
1382 r = r->next;
1385 return s;
1388 static void elf_section_header(int name, int type, int flags,
1389 void *data, bool is_saa, int32_t datalen,
1390 int link, int info, int align, int eltsize)
1392 elf_sects[elf_nsect].data = data;
1393 elf_sects[elf_nsect].len = datalen;
1394 elf_sects[elf_nsect].is_saa = is_saa;
1395 elf_nsect++;
1397 fwriteint32_t((int32_t)name, elffp);
1398 fwriteint32_t((int32_t)type, elffp);
1399 fwriteint32_t((int32_t)flags, elffp);
1400 fwriteint32_t(0L, elffp); /* no address, ever, in object files */
1401 fwriteint32_t(type == 0 ? 0L : elf_foffs, elffp);
1402 fwriteint32_t(datalen, elffp);
1403 if (data)
1404 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1405 fwriteint32_t((int32_t)link, elffp);
1406 fwriteint32_t((int32_t)info, elffp);
1407 fwriteint32_t((int32_t)align, elffp);
1408 fwriteint32_t((int32_t)eltsize, elffp);
1411 static void elf_write_sections(void)
1413 int i;
1414 for (i = 0; i < elf_nsect; i++)
1415 if (elf_sects[i].data) {
1416 int32_t len = elf_sects[i].len;
1417 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1418 int32_t align = reallen - len;
1419 if (elf_sects[i].is_saa)
1420 saa_fpwrite(elf_sects[i].data, elffp);
1421 else
1422 fwrite(elf_sects[i].data, len, 1, elffp);
1423 fwrite(align_str, align, 1, elffp);
1427 static void elf_sect_write(struct Section *sect,
1428 const uint8_t *data, uint32_t len)
1430 saa_wbytes(sect->data, data, len);
1431 sect->len += len;
1434 static int32_t elf_segbase(int32_t segment)
1436 return segment;
1439 static int elf_directive(char *directive, char *value, int pass)
1441 bool err;
1442 int64_t n;
1443 char *p;
1445 if (!strcmp(directive, "osabi")) {
1446 if (pass == 2)
1447 return 1; /* ignore in pass 2 */
1449 n = readnum(value, &err);
1450 if (err) {
1451 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1452 return 1;
1454 if (n < 0 || n > 255) {
1455 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1456 return 1;
1458 elf_osabi = n;
1459 elf_abiver = 0;
1461 if ((p = strchr(value,',')) == NULL)
1462 return 1;
1464 n = readnum(p+1, &err);
1465 if (err || n < 0 || n > 255) {
1466 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1467 return 1;
1470 elf_abiver = n;
1471 return 1;
1474 return 0;
1477 static void elf_filename(char *inname, char *outname, efunc error)
1479 strcpy(elf_module, inname);
1480 standard_extension(inname, outname, ".o", error);
1483 extern macros_t elf_stdmac[];
1485 static int elf_set_info(enum geninfo type, char **val)
1487 (void)type;
1488 (void)val;
1489 return 0;
1491 static struct dfmt df_dwarf = {
1492 "ELF32 (i386) dwarf debug format for Linux",
1493 "dwarf",
1494 debug32_init,
1495 dwarf32_linenum,
1496 debug32_deflabel,
1497 debug32_directive,
1498 debug32_typevalue,
1499 dwarf32_output,
1500 dwarf32_cleanup
1502 static struct dfmt df_stabs = {
1503 "ELF32 (i386) stabs debug format for Linux",
1504 "stabs",
1505 debug32_init,
1506 stabs32_linenum,
1507 debug32_deflabel,
1508 debug32_directive,
1509 debug32_typevalue,
1510 stabs32_output,
1511 stabs32_cleanup
1514 struct dfmt *elf32_debugs_arr[3] = { &df_stabs, &df_dwarf, NULL };
1516 struct ofmt of_elf32 = {
1517 "ELF32 (i386) object files (e.g. Linux)",
1518 "elf32",
1519 NULL,
1520 elf32_debugs_arr,
1521 &null_debug_form,
1522 elf_stdmac,
1523 elf_init,
1524 elf_set_info,
1525 elf_out,
1526 elf_deflabel,
1527 elf_section_names,
1528 elf_segbase,
1529 elf_directive,
1530 elf_filename,
1531 elf_cleanup
1534 struct ofmt of_elf = {
1535 "ELF (short name for ELF32) ",
1536 "elf",
1537 NULL,
1538 elf32_debugs_arr,
1539 &null_debug_form,
1540 elf_stdmac,
1541 elf_init,
1542 elf_set_info,
1543 elf_out,
1544 elf_deflabel,
1545 elf_section_names,
1546 elf_segbase,
1547 elf_directive,
1548 elf_filename,
1549 elf_cleanup
1551 /* again, the stabs debugging stuff (code) */
1553 void debug32_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1555 (void)of;
1556 (void)id;
1557 (void)fp;
1558 (void)error;
1561 void stabs32_linenum(const char *filename, int32_t linenumber, int32_t segto)
1563 (void)segto;
1565 if (!stabs_filename) {
1566 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1567 strcpy(stabs_filename, filename);
1568 } else {
1569 if (strcmp(stabs_filename, filename)) {
1570 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1571 in fact, this leak comes in quite handy to maintain a list of files
1572 encountered so far in the symbol lines... */
1574 /* why not nasm_free(stabs_filename); we're done with the old one */
1576 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1577 strcpy(stabs_filename, filename);
1580 debug_immcall = 1;
1581 currentline = linenumber;
1584 void debug32_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1585 char *special)
1587 (void)name;
1588 (void)segment;
1589 (void)offset;
1590 (void)is_global;
1591 (void)special;
1594 void debug32_directive(const char *directive, const char *params)
1596 (void)directive;
1597 (void)params;
1600 void debug32_typevalue(int32_t type)
1602 int32_t stype, ssize;
1603 switch (TYM_TYPE(type)) {
1604 case TY_LABEL:
1605 ssize = 0;
1606 stype = STT_NOTYPE;
1607 break;
1608 case TY_BYTE:
1609 ssize = 1;
1610 stype = STT_OBJECT;
1611 break;
1612 case TY_WORD:
1613 ssize = 2;
1614 stype = STT_OBJECT;
1615 break;
1616 case TY_DWORD:
1617 ssize = 4;
1618 stype = STT_OBJECT;
1619 break;
1620 case TY_FLOAT:
1621 ssize = 4;
1622 stype = STT_OBJECT;
1623 break;
1624 case TY_QWORD:
1625 ssize = 8;
1626 stype = STT_OBJECT;
1627 break;
1628 case TY_TBYTE:
1629 ssize = 10;
1630 stype = STT_OBJECT;
1631 break;
1632 case TY_OWORD:
1633 ssize = 8;
1634 stype = STT_OBJECT;
1635 break;
1636 case TY_COMMON:
1637 ssize = 0;
1638 stype = STT_COMMON;
1639 break;
1640 case TY_SEG:
1641 ssize = 0;
1642 stype = STT_SECTION;
1643 break;
1644 case TY_EXTERN:
1645 ssize = 0;
1646 stype = STT_NOTYPE;
1647 break;
1648 case TY_EQU:
1649 ssize = 0;
1650 stype = STT_NOTYPE;
1651 break;
1652 default:
1653 ssize = 0;
1654 stype = STT_NOTYPE;
1655 break;
1657 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1658 lastsym->size = ssize;
1659 lastsym->type = stype;
1663 void stabs32_output(int type, void *param)
1665 struct symlininfo *s;
1666 struct linelist *el;
1667 if (type == TY_STABSSYMLIN) {
1668 if (debug_immcall) {
1669 s = (struct symlininfo *)param;
1670 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1671 return; /* we are only interested in the text stuff */
1672 numlinestabs++;
1673 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1674 el->info.offset = s->offset;
1675 el->info.section = s->section;
1676 el->info.name = s->name;
1677 el->line = currentline;
1678 el->filename = stabs_filename;
1679 el->next = 0;
1680 if (stabslines) {
1681 stabslines->last->next = el;
1682 stabslines->last = el;
1683 } else {
1684 stabslines = el;
1685 stabslines->last = el;
1689 debug_immcall = 0;
1692 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1693 do {\
1694 WRITELONG(p,n_strx); \
1695 WRITECHAR(p,n_type); \
1696 WRITECHAR(p,n_other); \
1697 WRITESHORT(p,n_desc); \
1698 WRITELONG(p,n_value); \
1699 } while (0)
1701 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1703 void stabs32_generate(void)
1705 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1706 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1707 char **allfiles;
1708 int *fileidx;
1710 struct linelist *ptr;
1712 ptr = stabslines;
1714 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1715 for (i = 0; i < numlinestabs; i++)
1716 allfiles[i] = 0;
1717 numfiles = 0;
1718 while (ptr) {
1719 if (numfiles == 0) {
1720 allfiles[0] = ptr->filename;
1721 numfiles++;
1722 } else {
1723 for (i = 0; i < numfiles; i++) {
1724 if (!strcmp(allfiles[i], ptr->filename))
1725 break;
1727 if (i >= numfiles) {
1728 allfiles[i] = ptr->filename;
1729 numfiles++;
1732 ptr = ptr->next;
1734 strsize = 1;
1735 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1736 for (i = 0; i < numfiles; i++) {
1737 fileidx[i] = strsize;
1738 strsize += strlen(allfiles[i]) + 1;
1740 mainfileindex = 0;
1741 for (i = 0; i < numfiles; i++) {
1742 if (!strcmp(allfiles[i], elf_module)) {
1743 mainfileindex = i;
1744 break;
1748 /* worst case size of the stab buffer would be:
1749 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1751 sbuf =
1752 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1753 sizeof(struct stabentry));
1755 ssbuf = (uint8_t *)nasm_malloc(strsize);
1757 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1758 rptr = rbuf;
1760 for (i = 0; i < numfiles; i++) {
1761 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1763 ssbuf[0] = 0;
1765 stabstrlen = strsize; /* set global variable for length of stab strings */
1767 sptr = sbuf;
1768 ptr = stabslines;
1769 numstabs = 0;
1771 if (ptr) {
1772 /* this is the first stab, its strx points to the filename of the
1773 the source-file, the n_desc field should be set to the number
1774 of remaining stabs
1776 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1778 /* this is the stab for the main source file */
1779 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1781 /* relocation table entry */
1783 /* Since the symbol table has two entries before */
1784 /* the section symbols, the index in the info.section */
1785 /* member must be adjusted by adding 2 */
1787 WRITELONG(rptr, (sptr - sbuf) - 4);
1788 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1790 numstabs++;
1791 currfile = mainfileindex;
1794 while (ptr) {
1795 if (strcmp(allfiles[currfile], ptr->filename)) {
1796 /* oops file has changed... */
1797 for (i = 0; i < numfiles; i++)
1798 if (!strcmp(allfiles[i], ptr->filename))
1799 break;
1800 currfile = i;
1801 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1802 ptr->info.offset);
1803 numstabs++;
1805 /* relocation table entry */
1806 WRITELONG(rptr, (sptr - sbuf) - 4);
1807 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1810 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1811 numstabs++;
1813 /* relocation table entry */
1815 WRITELONG(rptr, (sptr - sbuf) - 4);
1816 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1818 ptr = ptr->next;
1822 ((struct stabentry *)sbuf)->n_desc = numstabs;
1824 nasm_free(allfiles);
1825 nasm_free(fileidx);
1827 stablen = (sptr - sbuf);
1828 stabrellen = (rptr - rbuf);
1829 stabrelbuf = rbuf;
1830 stabbuf = sbuf;
1831 stabstrbuf = ssbuf;
1834 void stabs32_cleanup(void)
1836 struct linelist *ptr, *del;
1837 if (!stabslines)
1838 return;
1839 ptr = stabslines;
1840 while (ptr) {
1841 del = ptr;
1842 ptr = ptr->next;
1843 nasm_free(del);
1845 if (stabbuf)
1846 nasm_free(stabbuf);
1847 if (stabrelbuf)
1848 nasm_free(stabrelbuf);
1849 if (stabstrbuf)
1850 nasm_free(stabstrbuf);
1852 /* dwarf routines */
1855 void dwarf32_linenum(const char *filename, int32_t linenumber, int32_t segto)
1857 (void)segto;
1858 dwarf32_findfile(filename);
1859 debug_immcall = 1;
1860 currentline = linenumber;
1863 /* called from elf_out with type == TY_DEBUGSYMLIN */
1864 void dwarf32_output(int type, void *param)
1866 int ln, aa, inx, maxln, soc;
1867 struct symlininfo *s;
1868 struct SAA *plinep;
1870 (void)type;
1872 s = (struct symlininfo *)param;
1873 /* line number info is only gathered for executable sections */
1874 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1875 return;
1876 /* Check if section index has changed */
1877 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1879 dwarf32_findsect(s->section);
1881 /* do nothing unless line or file has changed */
1882 if (debug_immcall)
1884 ln = currentline - dwarf_csect->line;
1885 aa = s->offset - dwarf_csect->offset;
1886 inx = dwarf_clist->line;
1887 plinep = dwarf_csect->psaa;
1888 /* check for file change */
1889 if (!(inx == dwarf_csect->file))
1891 saa_write8(plinep,DW_LNS_set_file);
1892 saa_write8(plinep,inx);
1893 dwarf_csect->file = inx;
1895 /* check for line change */
1896 if (ln)
1898 /* test if in range of special op code */
1899 maxln = line_base + line_range;
1900 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1901 if (ln >= line_base && ln < maxln && soc < 256)
1903 saa_write8(plinep,soc);
1905 else
1907 if (ln)
1909 saa_write8(plinep,DW_LNS_advance_line);
1910 saa_wleb128s(plinep,ln);
1912 if (aa)
1914 saa_write8(plinep,DW_LNS_advance_pc);
1915 saa_wleb128u(plinep,aa);
1918 dwarf_csect->line = currentline;
1919 dwarf_csect->offset = s->offset;
1921 /* show change handled */
1922 debug_immcall = 0;
1927 void dwarf32_generate(void)
1929 static const char nasm_signature[] = "NASM " NASM_VER;
1930 uint8_t *pbuf;
1931 int indx;
1932 struct linelist *ftentry;
1933 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1934 struct SAA *parangesrel, *plinesrel, *pinforel;
1935 struct sectlist *psect;
1936 size_t saalen, linepoff, totlen, highaddr;
1938 /* write epilogues for each line program range */
1939 /* and build aranges section */
1940 paranges = saa_init(1L);
1941 parangesrel = saa_init(1L);
1942 saa_write16(paranges,2); /* dwarf version */
1943 saa_write32(parangesrel, paranges->datalen+4);
1944 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
1945 saa_write32(parangesrel, 0);
1946 saa_write32(paranges,0); /* offset into info */
1947 saa_write8(paranges,4); /* pointer size */
1948 saa_write8(paranges,0); /* not segmented */
1949 saa_write32(paranges,0); /* padding */
1950 /* iterate though sectlist entries */
1951 psect = dwarf_fsect;
1952 totlen = 0;
1953 highaddr = 0;
1954 for (indx = 0; indx < dwarf_nsections; indx++)
1956 plinep = psect->psaa;
1957 /* Line Number Program Epilogue */
1958 saa_write8(plinep,2); /* std op 2 */
1959 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1960 saa_write8(plinep,DW_LNS_extended_op);
1961 saa_write8(plinep,1); /* operand length */
1962 saa_write8(plinep,DW_LNE_end_sequence);
1963 totlen += plinep->datalen;
1964 /* range table relocation entry */
1965 saa_write32(parangesrel, paranges->datalen + 4);
1966 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
1967 saa_write32(parangesrel, (uint32_t) 0);
1968 /* range table entry */
1969 saa_write32(paranges,0x0000); /* range start */
1970 saa_write32(paranges,sects[psect->section]->len); /* range length */
1971 highaddr += sects[psect->section]->len;
1972 /* done with this entry */
1973 psect = psect->next;
1975 saa_write32(paranges,0); /* null address */
1976 saa_write32(paranges,0); /* null length */
1977 saalen = paranges->datalen;
1978 arangeslen = saalen + 4;
1979 arangesbuf = pbuf = nasm_malloc(arangeslen);
1980 WRITELONG(pbuf,saalen); /* initial length */
1981 saa_rnbytes(paranges, pbuf, saalen);
1982 saa_free(paranges);
1984 /* build rela.aranges section */
1985 arangesrellen = saalen = parangesrel->datalen;
1986 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1987 saa_rnbytes(parangesrel, pbuf, saalen);
1988 saa_free(parangesrel);
1990 /* build pubnames section */
1991 ppubnames = saa_init(1L);
1992 saa_write16(ppubnames,3); /* dwarf version */
1993 saa_write32(ppubnames,0); /* offset into info */
1994 saa_write32(ppubnames,0); /* space used in info */
1995 saa_write32(ppubnames,0); /* end of list */
1996 saalen = ppubnames->datalen;
1997 pubnameslen = saalen + 4;
1998 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1999 WRITELONG(pbuf,saalen); /* initial length */
2000 saa_rnbytes(ppubnames, pbuf, saalen);
2001 saa_free(ppubnames);
2003 /* build info section */
2004 pinfo = saa_init(1L);
2005 pinforel = saa_init(1L);
2006 saa_write16(pinfo,2); /* dwarf version */
2007 saa_write32(pinforel, pinfo->datalen + 4);
2008 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
2009 saa_write32(pinforel, 0);
2010 saa_write32(pinfo,0); /* offset into abbrev */
2011 saa_write8(pinfo,4); /* pointer size */
2012 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2013 saa_write32(pinforel, pinfo->datalen + 4);
2014 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
2015 saa_write32(pinforel, 0);
2016 saa_write32(pinfo,0); /* DW_AT_low_pc */
2017 saa_write32(pinforel, pinfo->datalen + 4);
2018 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
2019 saa_write32(pinforel, 0);
2020 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
2021 saa_write32(pinforel, pinfo->datalen + 4);
2022 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
2023 saa_write32(pinforel, 0);
2024 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2025 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2026 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2027 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2028 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2029 saa_write32(pinforel, pinfo->datalen + 4);
2030 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
2031 saa_write32(pinforel, 0);
2032 saa_write32(pinfo,0); /* DW_AT_low_pc */
2033 saa_write32(pinfo,0); /* DW_AT_frame_base */
2034 saa_write8(pinfo,0); /* end of entries */
2035 saalen = pinfo->datalen;
2036 infolen = saalen + 4;
2037 infobuf = pbuf = nasm_malloc(infolen);
2038 WRITELONG(pbuf,saalen); /* initial length */
2039 saa_rnbytes(pinfo, pbuf, saalen);
2040 saa_free(pinfo);
2042 /* build rela.info section */
2043 inforellen = saalen = pinforel->datalen;
2044 inforelbuf = pbuf = nasm_malloc(inforellen);
2045 saa_rnbytes(pinforel, pbuf, saalen);
2046 saa_free(pinforel);
2048 /* build abbrev section */
2049 pabbrev = saa_init(1L);
2050 saa_write8(pabbrev,1); /* entry number LEB128u */
2051 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
2052 saa_write8(pabbrev,1); /* has children */
2053 /* the following attributes and forms are all LEB128u values */
2054 saa_write8(pabbrev,DW_AT_low_pc);
2055 saa_write8(pabbrev,DW_FORM_addr);
2056 saa_write8(pabbrev,DW_AT_high_pc);
2057 saa_write8(pabbrev,DW_FORM_addr);
2058 saa_write8(pabbrev,DW_AT_stmt_list);
2059 saa_write8(pabbrev,DW_FORM_data4);
2060 saa_write8(pabbrev,DW_AT_name);
2061 saa_write8(pabbrev,DW_FORM_string);
2062 saa_write8(pabbrev,DW_AT_producer);
2063 saa_write8(pabbrev,DW_FORM_string);
2064 saa_write8(pabbrev,DW_AT_language);
2065 saa_write8(pabbrev,DW_FORM_data2);
2066 saa_write16(pabbrev,0); /* end of entry */
2067 /* LEB128u usage same as above */
2068 saa_write8(pabbrev,2); /* entry number */
2069 saa_write8(pabbrev,DW_TAG_subprogram);
2070 saa_write8(pabbrev,0); /* no children */
2071 saa_write8(pabbrev,DW_AT_low_pc);
2072 saa_write8(pabbrev,DW_FORM_addr);
2073 saa_write8(pabbrev,DW_AT_frame_base);
2074 saa_write8(pabbrev,DW_FORM_data4);
2075 saa_write16(pabbrev,0); /* end of entry */
2076 abbrevlen = saalen = pabbrev->datalen;
2077 abbrevbuf = pbuf = nasm_malloc(saalen);
2078 saa_rnbytes(pabbrev, pbuf, saalen);
2079 saa_free(pabbrev);
2081 /* build line section */
2082 /* prolog */
2083 plines = saa_init(1L);
2084 saa_write8(plines,1); /* Minimum Instruction Length */
2085 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2086 saa_write8(plines,line_base); /* Line Base */
2087 saa_write8(plines,line_range); /* Line Range */
2088 saa_write8(plines,opcode_base); /* Opcode Base */
2089 /* standard opcode lengths (# of LEB128u operands) */
2090 saa_write8(plines,0); /* Std opcode 1 length */
2091 saa_write8(plines,1); /* Std opcode 2 length */
2092 saa_write8(plines,1); /* Std opcode 3 length */
2093 saa_write8(plines,1); /* Std opcode 4 length */
2094 saa_write8(plines,1); /* Std opcode 5 length */
2095 saa_write8(plines,0); /* Std opcode 6 length */
2096 saa_write8(plines,0); /* Std opcode 7 length */
2097 saa_write8(plines,0); /* Std opcode 8 length */
2098 saa_write8(plines,1); /* Std opcode 9 length */
2099 saa_write8(plines,0); /* Std opcode 10 length */
2100 saa_write8(plines,0); /* Std opcode 11 length */
2101 saa_write8(plines,1); /* Std opcode 12 length */
2102 /* Directory Table */
2103 saa_write8(plines,0); /* End of table */
2104 /* File Name Table */
2105 ftentry = dwarf_flist;
2106 for (indx = 0;indx<dwarf_numfiles;indx++)
2108 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2109 saa_write8(plines,0); /* directory LEB128u */
2110 saa_write8(plines,0); /* time LEB128u */
2111 saa_write8(plines,0); /* size LEB128u */
2112 ftentry = ftentry->next;
2114 saa_write8(plines,0); /* End of table */
2115 linepoff = plines->datalen;
2116 linelen = linepoff + totlen + 10;
2117 linebuf = pbuf = nasm_malloc(linelen);
2118 WRITELONG(pbuf,linelen-4); /* initial length */
2119 WRITESHORT(pbuf,3); /* dwarf version */
2120 WRITELONG(pbuf,linepoff); /* offset to line number program */
2121 /* write line header */
2122 saalen = linepoff;
2123 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2124 pbuf += linepoff;
2125 saa_free(plines);
2126 /* concatonate line program ranges */
2127 linepoff += 13;
2128 plinesrel = saa_init(1L);
2129 psect = dwarf_fsect;
2130 for (indx = 0; indx < dwarf_nsections; indx++)
2132 saa_write32(plinesrel, linepoff);
2133 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
2134 saa_write32(plinesrel, (uint32_t) 0);
2135 plinep = psect->psaa;
2136 saalen = plinep->datalen;
2137 saa_rnbytes(plinep, pbuf, saalen);
2138 pbuf += saalen;
2139 linepoff += saalen;
2140 saa_free(plinep);
2141 /* done with this entry */
2142 psect = psect->next;
2146 /* build rela.lines section */
2147 linerellen =saalen = plinesrel->datalen;
2148 linerelbuf = pbuf = nasm_malloc(linerellen);
2149 saa_rnbytes(plinesrel, pbuf, saalen);
2150 saa_free(plinesrel);
2152 /* build frame section */
2153 framelen = 4;
2154 framebuf = pbuf = nasm_malloc(framelen);
2155 WRITELONG(pbuf,framelen-4); /* initial length */
2157 /* build loc section */
2158 loclen = 16;
2159 locbuf = pbuf = nasm_malloc(loclen);
2160 WRITELONG(pbuf,0); /* null beginning offset */
2161 WRITELONG(pbuf,0); /* null ending offset */
2164 void dwarf32_cleanup(void)
2166 if (arangesbuf)
2167 nasm_free(arangesbuf);
2168 if (arangesrelbuf)
2169 nasm_free(arangesrelbuf);
2170 if (pubnamesbuf)
2171 nasm_free(pubnamesbuf);
2172 if (infobuf)
2173 nasm_free(infobuf);
2174 if (inforelbuf)
2175 nasm_free(inforelbuf);
2176 if (abbrevbuf)
2177 nasm_free(abbrevbuf);
2178 if (linebuf)
2179 nasm_free(linebuf);
2180 if (linerelbuf)
2181 nasm_free(linerelbuf);
2182 if (framebuf)
2183 nasm_free(framebuf);
2184 if (locbuf)
2185 nasm_free(locbuf);
2187 void dwarf32_findfile(const char * fname)
2189 int finx;
2190 struct linelist *match;
2192 /* return if fname is current file name */
2193 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2194 /* search for match */
2195 else
2197 match = 0;
2198 if (dwarf_flist)
2200 match = dwarf_flist;
2201 for (finx = 0; finx < dwarf_numfiles; finx++)
2203 if (!(strcmp(fname, match->filename)))
2205 dwarf_clist = match;
2206 return;
2210 /* add file name to end of list */
2211 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2212 dwarf_numfiles++;
2213 dwarf_clist->line = dwarf_numfiles;
2214 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2215 strcpy(dwarf_clist->filename,fname);
2216 dwarf_clist->next = 0;
2217 /* if first entry */
2218 if (!dwarf_flist)
2220 dwarf_flist = dwarf_elist = dwarf_clist;
2221 dwarf_clist->last = 0;
2223 /* chain to previous entry */
2224 else
2226 dwarf_elist->next = dwarf_clist;
2227 dwarf_elist = dwarf_clist;
2231 /* */
2232 void dwarf32_findsect(const int index)
2234 int sinx;
2235 struct sectlist *match;
2236 struct SAA *plinep;
2237 /* return if index is current section index */
2238 if (dwarf_csect && (dwarf_csect->section == index))
2240 return;
2242 /* search for match */
2243 else
2245 match = 0;
2246 if (dwarf_fsect)
2248 match = dwarf_fsect;
2249 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2251 if ((match->section == index))
2253 dwarf_csect = match;
2254 return;
2256 match = match->next;
2259 /* add entry to end of list */
2260 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2261 dwarf_nsections++;
2262 dwarf_csect->psaa = plinep = saa_init(1L);
2263 dwarf_csect->line = 1;
2264 dwarf_csect->offset = 0;
2265 dwarf_csect->file = 1;
2266 dwarf_csect->section = index;
2267 dwarf_csect->next = 0;
2268 /* set relocatable address at start of line program */
2269 saa_write8(plinep,DW_LNS_extended_op);
2270 saa_write8(plinep,5); /* operand length */
2271 saa_write8(plinep,DW_LNE_set_address);
2272 saa_write32(plinep,0); /* Start Address */
2273 /* if first entry */
2274 if (!dwarf_fsect)
2276 dwarf_fsect = dwarf_esect = dwarf_csect;
2277 dwarf_csect->last = 0;
2279 /* chain to previous entry */
2280 else
2282 dwarf_esect->next = dwarf_csect;
2283 dwarf_esect = dwarf_csect;
2288 #endif /* OF_ELF */