Use term "host platform" consistently
[nasm.git] / output / outelf32.c
blobc11c4e40dbefa508d5b40c7b3709eb6eef330490
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 "stdscan.h"
21 #include "outform.h"
23 #ifdef OF_ELF32
26 * Relocation types.
28 enum reloc_type {
29 R_386_32 = 1, /* ordinary absolute relocation */
30 R_386_PC32 = 2, /* PC-relative relocation */
31 R_386_GOT32 = 3, /* an offset into GOT */
32 R_386_PLT32 = 4, /* a PC-relative offset into PLT */
33 R_386_COPY = 5, /* ??? */
34 R_386_GLOB_DAT = 6, /* ??? */
35 R_386_JUMP_SLOT = 7, /* ??? */
36 R_386_RELATIVE = 8, /* ??? */
37 R_386_GOTOFF = 9, /* an offset from GOT base */
38 R_386_GOTPC = 10, /* a PC-relative offset _to_ GOT */
39 /* These are GNU extensions, but useful */
40 R_386_16 = 20, /* A 16-bit absolute relocation */
41 R_386_PC16 = 21, /* A 16-bit PC-relative relocation */
42 R_386_8 = 22, /* An 8-bit absolute relocation */
43 R_386_PC8 = 23 /* An 8-bit PC-relative relocation */
46 struct Reloc {
47 struct Reloc *next;
48 int32_t address; /* relative to _start_ of section */
49 int32_t symbol; /* symbol index */
50 int type; /* type of relocation */
53 struct Symbol {
54 int32_t strpos; /* string table position of name */
55 int32_t section; /* section ID of the symbol */
56 int type; /* symbol type */
57 int other; /* symbol visibility */
58 int32_t value; /* address, or COMMON variable align */
59 int32_t size; /* size of symbol */
60 int32_t globnum; /* symbol table offset if global */
61 struct Symbol *next; /* list of globals in each section */
62 struct Symbol *nextfwd; /* list of unresolved-size symbols */
63 char *name; /* used temporarily if in above list */
66 #define SHT_PROGBITS 1
67 #define SHT_NOBITS 8
69 #define SHF_WRITE 1
70 #define SHF_ALLOC 2
71 #define SHF_EXECINSTR 4
73 struct Section {
74 struct SAA *data;
75 uint32_t len, size, nrelocs;
76 int32_t index;
77 int type; /* SHT_PROGBITS or SHT_NOBITS */
78 int align; /* alignment: power of two */
79 uint32_t flags; /* section flags */
80 char *name;
81 struct SAA *rel;
82 int32_t rellen;
83 struct Reloc *head, **tail;
84 struct Symbol *gsyms; /* global symbols in section */
87 #define SECT_DELTA 32
88 static struct Section **sects;
89 static int nsects, sectlen;
91 #define SHSTR_DELTA 256
92 static char *shstrtab;
93 static int shstrtablen, shstrtabsize;
95 static struct SAA *syms;
96 static uint32_t nlocals, nglobs;
98 static int32_t def_seg;
100 static struct RAA *bsym;
102 static struct SAA *strs;
103 static uint32_t strslen;
105 static FILE *elffp;
106 static efunc error;
107 static evalfunc evaluate;
109 static struct Symbol *fwds;
111 static char elf_module[FILENAME_MAX];
113 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
114 static uint8_t elf_abiver = 0; /* Current ABI version */
116 extern struct ofmt of_elf32;
117 extern struct ofmt of_elf;
119 #define SHN_ABS 0xFFF1
120 #define SHN_COMMON 0xFFF2
121 #define SHN_UNDEF 0
123 #define SYM_GLOBAL 0x10
125 #define STT_NOTYPE 0 /* Symbol type is unspecified */
126 #define STT_OBJECT 1 /* Symbol is a data object */
127 #define STT_FUNC 2 /* Symbol is a code object */
128 #define STT_SECTION 3 /* Symbol associated with a section */
129 #define STT_FILE 4 /* Symbol's name is file name */
130 #define STT_COMMON 5 /* Symbol is a common data object */
131 #define STT_TLS 6 /* Symbol is thread-local data object*/
132 #define STT_NUM 7 /* Number of defined types. */
134 #define STV_DEFAULT 0
135 #define STV_INTERNAL 1
136 #define STV_HIDDEN 2
137 #define STV_PROTECTED 3
139 #define GLOBAL_TEMP_BASE 1048576 /* bigger than any reasonable sym id */
141 #define SEG_ALIGN 16 /* alignment of sections in file */
142 #define SEG_ALIGN_1 (SEG_ALIGN-1)
144 static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
146 static struct ELF_SECTDATA {
147 void *data;
148 int32_t len;
149 bool is_saa;
150 } *elf_sects;
151 static int elf_nsect;
152 static int32_t elf_foffs;
154 static void elf_write(void);
155 static void elf_sect_write(struct Section *, const uint8_t *,
156 uint32_t);
157 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
158 int, int);
159 static void elf_write_sections(void);
160 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
161 static struct SAA *elf_build_reltab(int32_t *, struct Reloc *);
162 static void add_sectname(char *, char *);
164 /* this stuff is needed for the stabs debugging format */
165 #define N_SO 0x64 /* ID for main source file */
166 #define N_SOL 0x84 /* ID for sub-source file */
167 #define N_BINCL 0x82
168 #define N_EINCL 0xA2
169 #define N_SLINE 0x44
170 #define TY_STABSSYMLIN 0x40 /* ouch */
172 struct stabentry {
173 uint32_t n_strx;
174 uint8_t n_type;
175 uint8_t n_other;
176 uint16_t n_desc;
177 uint32_t n_value;
180 struct erel {
181 int offset, info;
184 struct symlininfo {
185 int offset;
186 int section; /* section index */
187 char *name; /* shallow-copied pointer of section name */
190 struct linelist {
191 struct symlininfo info;
192 int line;
193 char *filename;
194 struct linelist *next;
195 struct linelist *last;
198 static struct linelist *stabslines = 0;
199 static int stabs_immcall = 0;
200 static int currentline = 0;
201 static int numlinestabs = 0;
202 static char *stabs_filename = 0;
203 static int symtabsection;
204 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
205 static int stablen, stabstrlen, stabrellen;
207 static struct dfmt df_stabs;
208 static struct Symbol *lastsym;
210 void stabs32_init(struct ofmt *, void *, FILE *, efunc);
211 void stabs32_linenum(const char *filename, int32_t linenumber, int32_t);
212 void stabs32_deflabel(char *, int32_t, int64_t, int, char *);
213 void stabs32_directive(const char *, const char *);
214 void stabs32_typevalue(int32_t);
215 void stabs32_output(int, void *);
216 void stabs32_generate(void);
217 void stabs32_cleanup(void);
219 /* end of stabs debugging stuff */
222 * Special section numbers which are used to define ELF special
223 * symbols, which can be used with WRT to provide PIC relocation
224 * types.
226 static int32_t elf_gotpc_sect, elf_gotoff_sect;
227 static int32_t elf_got_sect, elf_plt_sect;
228 static int32_t elf_sym_sect;
230 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
232 if (of_elf.current_dfmt != &null_debug_form)
233 of_elf32.current_dfmt = of_elf.current_dfmt;
234 elffp = fp;
235 error = errfunc;
236 evaluate = eval;
237 (void)ldef; /* placate optimisers */
238 sects = NULL;
239 nsects = sectlen = 0;
240 syms = saa_init((int32_t)sizeof(struct Symbol));
241 nlocals = nglobs = 0;
242 bsym = raa_init();
243 strs = saa_init(1L);
244 saa_wbytes(strs, "\0", 1L);
245 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
246 strslen = 2 + strlen(elf_module);
247 shstrtab = NULL;
248 shstrtablen = shstrtabsize = 0;;
249 add_sectname("", "");
251 fwds = NULL;
253 elf_gotpc_sect = seg_alloc();
254 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf32,
255 error);
256 elf_gotoff_sect = seg_alloc();
257 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf32,
258 error);
259 elf_got_sect = seg_alloc();
260 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf32,
261 error);
262 elf_plt_sect = seg_alloc();
263 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf32,
264 error);
265 elf_sym_sect = seg_alloc();
266 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf32,
267 error);
269 def_seg = seg_alloc();
272 static void elf_cleanup(int debuginfo)
274 struct Reloc *r;
275 int i;
277 (void)debuginfo;
279 elf_write();
280 fclose(elffp);
281 for (i = 0; i < nsects; i++) {
282 if (sects[i]->type != SHT_NOBITS)
283 saa_free(sects[i]->data);
284 if (sects[i]->head)
285 saa_free(sects[i]->rel);
286 while (sects[i]->head) {
287 r = sects[i]->head;
288 sects[i]->head = sects[i]->head->next;
289 nasm_free(r);
292 nasm_free(sects);
293 saa_free(syms);
294 raa_free(bsym);
295 saa_free(strs);
296 if (of_elf32.current_dfmt) {
297 of_elf32.current_dfmt->cleanup();
301 static void add_sectname(char *firsthalf, char *secondhalf)
303 int len = strlen(firsthalf) + strlen(secondhalf);
304 while (shstrtablen + len + 1 > shstrtabsize)
305 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
306 strcpy(shstrtab + shstrtablen, firsthalf);
307 strcat(shstrtab + shstrtablen, secondhalf);
308 shstrtablen += len + 1;
311 static int elf_make_section(char *name, int type, int flags, int align)
313 struct Section *s;
315 s = nasm_malloc(sizeof(*s));
317 if (type != SHT_NOBITS)
318 s->data = saa_init(1L);
319 s->head = NULL;
320 s->tail = &s->head;
321 s->len = s->size = 0;
322 s->nrelocs = 0;
323 if (!strcmp(name, ".text"))
324 s->index = def_seg;
325 else
326 s->index = seg_alloc();
327 add_sectname("", name);
328 s->name = nasm_malloc(1 + strlen(name));
329 strcpy(s->name, name);
330 s->type = type;
331 s->flags = flags;
332 s->align = align;
333 s->gsyms = NULL;
335 if (nsects >= sectlen)
336 sects =
337 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
338 sects[nsects++] = s;
340 return nsects - 1;
343 static int32_t elf_section_names(char *name, int pass, int *bits)
345 char *p;
346 unsigned flags_and, flags_or;
347 int type, align, i;
350 * Default is 32 bits.
352 if (!name) {
353 *bits = 32;
354 return def_seg;
357 p = name;
358 while (*p && !isspace(*p))
359 p++;
360 if (*p)
361 *p++ = '\0';
362 flags_and = flags_or = type = align = 0;
364 while (*p && isspace(*p))
365 p++;
366 while (*p) {
367 char *q = p;
368 while (*p && !isspace(*p))
369 p++;
370 if (*p)
371 *p++ = '\0';
372 while (*p && isspace(*p))
373 p++;
375 if (!nasm_strnicmp(q, "align=", 6)) {
376 align = atoi(q + 6);
377 if (align == 0)
378 align = 1;
379 if ((align - 1) & align) { /* means it's not a power of two */
380 error(ERR_NONFATAL, "section alignment %d is not"
381 " a power of two", align);
382 align = 1;
384 } else if (!nasm_stricmp(q, "alloc")) {
385 flags_and |= SHF_ALLOC;
386 flags_or |= SHF_ALLOC;
387 } else if (!nasm_stricmp(q, "noalloc")) {
388 flags_and |= SHF_ALLOC;
389 flags_or &= ~SHF_ALLOC;
390 } else if (!nasm_stricmp(q, "exec")) {
391 flags_and |= SHF_EXECINSTR;
392 flags_or |= SHF_EXECINSTR;
393 } else if (!nasm_stricmp(q, "noexec")) {
394 flags_and |= SHF_EXECINSTR;
395 flags_or &= ~SHF_EXECINSTR;
396 } else if (!nasm_stricmp(q, "write")) {
397 flags_and |= SHF_WRITE;
398 flags_or |= SHF_WRITE;
399 } else if (!nasm_stricmp(q, "nowrite")) {
400 flags_and |= SHF_WRITE;
401 flags_or &= ~SHF_WRITE;
402 } else if (!nasm_stricmp(q, "progbits")) {
403 type = SHT_PROGBITS;
404 } else if (!nasm_stricmp(q, "nobits")) {
405 type = SHT_NOBITS;
409 if (!strcmp(name, ".comment") ||
410 !strcmp(name, ".shstrtab") ||
411 !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
412 error(ERR_NONFATAL, "attempt to redefine reserved section"
413 "name `%s'", name);
414 return NO_SEG;
417 for (i = 0; i < nsects; i++)
418 if (!strcmp(name, sects[i]->name))
419 break;
420 if (i == nsects) {
421 if (!strcmp(name, ".text"))
422 i = elf_make_section(name, SHT_PROGBITS,
423 SHF_ALLOC | SHF_EXECINSTR, 16);
424 else if (!strcmp(name, ".rodata"))
425 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
426 else if (!strcmp(name, ".data"))
427 i = elf_make_section(name, SHT_PROGBITS,
428 SHF_ALLOC | SHF_WRITE, 4);
429 else if (!strcmp(name, ".bss"))
430 i = elf_make_section(name, SHT_NOBITS,
431 SHF_ALLOC | SHF_WRITE, 4);
432 else
433 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
434 if (type)
435 sects[i]->type = type;
436 if (align)
437 sects[i]->align = align;
438 sects[i]->flags &= ~flags_and;
439 sects[i]->flags |= flags_or;
440 } else if (pass == 1) {
441 if ((type && sects[i]->type != type)
442 || (align && sects[i]->align != align)
443 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
444 error(ERR_WARNING, "section attributes ignored on"
445 " redeclaration of section `%s'", name);
448 return sects[i]->index;
451 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
452 int is_global, char *special)
454 int pos = strslen;
455 struct Symbol *sym;
456 bool special_used = false;
458 #if defined(DEBUG) && DEBUG>2
459 fprintf(stderr,
460 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
461 name, segment, offset, is_global, special);
462 #endif
463 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
465 * This is a NASM special symbol. We never allow it into
466 * the ELF symbol table, even if it's a valid one. If it
467 * _isn't_ a valid one, we should barf immediately.
469 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
470 strcmp(name, "..got") && strcmp(name, "..plt") &&
471 strcmp(name, "..sym"))
472 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
473 return;
476 if (is_global == 3) {
477 struct Symbol **s;
479 * Fix up a forward-reference symbol size from the first
480 * pass.
482 for (s = &fwds; *s; s = &(*s)->nextfwd)
483 if (!strcmp((*s)->name, name)) {
484 struct tokenval tokval;
485 expr *e;
486 char *p = special;
488 while (*p && !isspace(*p))
489 p++;
490 while (*p && isspace(*p))
491 p++;
492 stdscan_reset();
493 stdscan_bufptr = p;
494 tokval.t_type = TOKEN_INVALID;
495 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
496 if (e) {
497 if (!is_simple(e))
498 error(ERR_NONFATAL, "cannot use relocatable"
499 " expression as symbol size");
500 else
501 (*s)->size = reloc_value(e);
505 * Remove it from the list of unresolved sizes.
507 nasm_free((*s)->name);
508 *s = (*s)->nextfwd;
509 return;
511 return; /* it wasn't an important one */
514 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
515 strslen += 1 + strlen(name);
517 lastsym = sym = saa_wstruct(syms);
519 sym->strpos = pos;
520 sym->type = is_global ? SYM_GLOBAL : 0;
521 sym->other = STV_DEFAULT;
522 sym->size = 0;
523 if (segment == NO_SEG)
524 sym->section = SHN_ABS;
525 else {
526 int i;
527 sym->section = SHN_UNDEF;
528 if (nsects == 0 && segment == def_seg) {
529 int tempint;
530 if (segment != elf_section_names(".text", 2, &tempint))
531 error(ERR_PANIC,
532 "strange segment conditions in ELF driver");
533 sym->section = nsects;
534 } else {
535 for (i = 0; i < nsects; i++)
536 if (segment == sects[i]->index) {
537 sym->section = i + 1;
538 break;
543 if (is_global == 2) {
544 sym->size = offset;
545 sym->value = 0;
546 sym->section = SHN_COMMON;
548 * We have a common variable. Check the special text to see
549 * if it's a valid number and power of two; if so, store it
550 * as the alignment for the common variable.
552 if (special) {
553 bool err;
554 sym->value = readnum(special, &err);
555 if (err)
556 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
557 " valid number", special);
558 else if ((sym->value | (sym->value - 1)) != 2 * sym->value - 1)
559 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
560 " power of two", special);
562 special_used = true;
563 } else
564 sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
566 if (sym->type == SYM_GLOBAL) {
568 * If sym->section == SHN_ABS, then the first line of the
569 * else section would cause a core dump, because its a reference
570 * beyond the end of the section array.
571 * This behaviour is exhibited by this code:
572 * GLOBAL crash_nasm
573 * crash_nasm equ 0
574 * To avoid such a crash, such requests are silently discarded.
575 * This may not be the best solution.
577 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
578 bsym = raa_write(bsym, segment, nglobs);
579 } else if (sym->section != SHN_ABS) {
581 * This is a global symbol; so we must add it to the linked
582 * list of global symbols in its section. We'll push it on
583 * the beginning of the list, because it doesn't matter
584 * much which end we put it on and it's easier like this.
586 * In addition, we check the special text for symbol
587 * type and size information.
589 sym->next = sects[sym->section - 1]->gsyms;
590 sects[sym->section - 1]->gsyms = sym;
592 if (special) {
593 int n = strcspn(special, " \t");
595 if (!nasm_strnicmp(special, "function", n))
596 sym->type |= STT_FUNC;
597 else if (!nasm_strnicmp(special, "data", n) ||
598 !nasm_strnicmp(special, "object", n))
599 sym->type |= STT_OBJECT;
600 else if (!nasm_strnicmp(special, "notype", n))
601 sym->type |= STT_NOTYPE;
602 else
603 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
604 n, special);
605 special += n;
607 while (isspace(*special))
608 ++special;
609 if (*special) {
610 n = strcspn(special, " \t");
611 if (!nasm_strnicmp(special, "default", n))
612 sym->other = STV_DEFAULT;
613 else if (!nasm_strnicmp(special, "internal", n))
614 sym->other = STV_INTERNAL;
615 else if (!nasm_strnicmp(special, "hidden", n))
616 sym->other = STV_HIDDEN;
617 else if (!nasm_strnicmp(special, "protected", n))
618 sym->other = STV_PROTECTED;
619 else
620 n = 0;
621 special += n;
624 if (*special) {
625 struct tokenval tokval;
626 expr *e;
627 int fwd = 0;
628 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
630 while (special[n] && isspace(special[n]))
631 n++;
633 * We have a size expression; attempt to
634 * evaluate it.
636 stdscan_reset();
637 stdscan_bufptr = special + n;
638 tokval.t_type = TOKEN_INVALID;
639 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
640 NULL);
641 if (fwd) {
642 sym->nextfwd = fwds;
643 fwds = sym;
644 sym->name = nasm_strdup(name);
645 } else if (e) {
646 if (!is_simple(e))
647 error(ERR_NONFATAL, "cannot use relocatable"
648 " expression as symbol size");
649 else
650 sym->size = reloc_value(e);
652 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
654 special_used = true;
657 sym->globnum = nglobs;
658 nglobs++;
659 } else
660 nlocals++;
662 if (special && !special_used)
663 error(ERR_NONFATAL, "no special symbol features supported here");
666 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
668 struct Reloc *r;
670 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
671 sect->tail = &r->next;
672 r->next = NULL;
674 r->address = sect->len;
675 if (segment == NO_SEG)
676 r->symbol = 0;
677 else {
678 int i;
679 r->symbol = 0;
680 for (i = 0; i < nsects; i++)
681 if (segment == sects[i]->index)
682 r->symbol = i + 2;
683 if (!r->symbol)
684 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
686 r->type = type;
688 sect->nrelocs++;
692 * This routine deals with ..got and ..sym relocations: the more
693 * complicated kinds. In shared-library writing, some relocations
694 * with respect to global symbols must refer to the precise symbol
695 * rather than referring to an offset from the base of the section
696 * _containing_ the symbol. Such relocations call to this routine,
697 * which searches the symbol list for the symbol in question.
699 * R_386_GOT32 references require the _exact_ symbol address to be
700 * used; R_386_32 references can be at an offset from the symbol.
701 * The boolean argument `exact' tells us this.
703 * Return value is the adjusted value of `addr', having become an
704 * offset from the symbol rather than the section. Should always be
705 * zero when returning from an exact call.
707 * Limitation: if you define two symbols at the same place,
708 * confusion will occur.
710 * Inefficiency: we search, currently, using a linked list which
711 * isn't even necessarily sorted.
713 static int32_t elf_add_gsym_reloc(struct Section *sect,
714 int32_t segment, int32_t offset,
715 int type, bool exact)
717 struct Reloc *r;
718 struct Section *s;
719 struct Symbol *sym, *sm;
720 int i;
723 * First look up the segment/offset pair and find a global
724 * symbol corresponding to it. If it's not one of our segments,
725 * then it must be an external symbol, in which case we're fine
726 * doing a normal elf_add_reloc after first sanity-checking
727 * that the offset from the symbol is zero.
729 s = NULL;
730 for (i = 0; i < nsects; i++)
731 if (segment == sects[i]->index) {
732 s = sects[i];
733 break;
735 if (!s) {
736 if (exact && offset != 0)
737 error(ERR_NONFATAL, "unable to find a suitable global symbol"
738 " for this reference");
739 else
740 elf_add_reloc(sect, segment, type);
741 return offset;
744 if (exact) {
746 * Find a symbol pointing _exactly_ at this one.
748 for (sym = s->gsyms; sym; sym = sym->next)
749 if (sym->value == offset)
750 break;
751 } else {
753 * Find the nearest symbol below this one.
755 sym = NULL;
756 for (sm = s->gsyms; sm; sm = sm->next)
757 if (sm->value <= offset && (!sym || sm->value > sym->value))
758 sym = sm;
760 if (!sym && exact) {
761 error(ERR_NONFATAL, "unable to find a suitable global symbol"
762 " for this reference");
763 return 0;
766 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
767 sect->tail = &r->next;
768 r->next = NULL;
770 r->address = sect->len;
771 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
772 r->type = type;
774 sect->nrelocs++;
776 return offset - sym->value;
779 static void elf_out(int32_t segto, const void *data,
780 enum out_type type, uint64_t size,
781 int32_t segment, int32_t wrt)
783 struct Section *s;
784 int32_t addr;
785 uint8_t mydata[4], *p;
786 int i;
787 static struct symlininfo sinfo;
790 * handle absolute-assembly (structure definitions)
792 if (segto == NO_SEG) {
793 if (type != OUT_RESERVE)
794 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
795 " space");
796 return;
799 s = NULL;
800 for (i = 0; i < nsects; i++)
801 if (segto == sects[i]->index) {
802 s = sects[i];
803 break;
805 if (!s) {
806 int tempint; /* ignored */
807 if (segto != elf_section_names(".text", 2, &tempint))
808 error(ERR_PANIC, "strange segment conditions in ELF driver");
809 else {
810 s = sects[nsects - 1];
811 i = nsects - 1;
815 /* again some stabs debugging stuff */
816 if (of_elf32.current_dfmt) {
817 sinfo.offset = s->len;
818 sinfo.section = i;
819 sinfo.name = s->name;
820 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
822 /* end of debugging stuff */
824 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
825 error(ERR_WARNING, "attempt to initialize memory in"
826 " BSS section `%s': ignored", s->name);
827 if (type == OUT_REL2ADR)
828 size = 2;
829 else if (type == OUT_REL4ADR)
830 size = 4;
831 s->len += size;
832 return;
835 if (type == OUT_RESERVE) {
836 if (s->type == SHT_PROGBITS) {
837 error(ERR_WARNING, "uninitialized space declared in"
838 " non-BSS section `%s': zeroing", s->name);
839 elf_sect_write(s, NULL, size);
840 } else
841 s->len += size;
842 } else if (type == OUT_RAWDATA) {
843 if (segment != NO_SEG)
844 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
845 elf_sect_write(s, data, size);
846 } else if (type == OUT_ADDRESS) {
847 bool gnu16 = false;
848 addr = *(int64_t *)data;
849 if (segment != NO_SEG) {
850 if (segment % 2) {
851 error(ERR_NONFATAL, "ELF format does not support"
852 " segment base references");
853 } else {
854 if (wrt == NO_SEG) {
855 if (size == 2) {
856 gnu16 = true;
857 elf_add_reloc(s, segment, R_386_16);
858 } else {
859 elf_add_reloc(s, segment, R_386_32);
861 } else if (wrt == elf_gotpc_sect + 1) {
863 * The user will supply GOT relative to $$. ELF
864 * will let us have GOT relative to $. So we
865 * need to fix up the data item by $-$$.
867 addr += s->len;
868 elf_add_reloc(s, segment, R_386_GOTPC);
869 } else if (wrt == elf_gotoff_sect + 1) {
870 elf_add_reloc(s, segment, R_386_GOTOFF);
871 } else if (wrt == elf_got_sect + 1) {
872 addr = elf_add_gsym_reloc(s, segment, addr,
873 R_386_GOT32, true);
874 } else if (wrt == elf_sym_sect + 1) {
875 if (size == 2) {
876 gnu16 = true;
877 addr = elf_add_gsym_reloc(s, segment, addr,
878 R_386_16, false);
879 } else {
880 addr = elf_add_gsym_reloc(s, segment, addr,
881 R_386_32, false);
883 } else if (wrt == elf_plt_sect + 1) {
884 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
885 "relative PLT references");
886 } else {
887 error(ERR_NONFATAL, "ELF format does not support this"
888 " use of WRT");
889 wrt = NO_SEG; /* we can at least _try_ to continue */
893 p = mydata;
894 if (gnu16) {
895 error(ERR_WARNING | ERR_WARN_GNUELF,
896 "16-bit relocations in ELF is a GNU extension");
897 WRITESHORT(p, addr);
898 } else {
899 if (size != 4 && segment != NO_SEG) {
900 error(ERR_NONFATAL,
901 "Unsupported non-32-bit ELF relocation");
903 WRITELONG(p, addr);
905 elf_sect_write(s, mydata, size);
906 } else if (type == OUT_REL2ADR) {
907 if (segment == segto)
908 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
909 if (segment != NO_SEG && segment % 2) {
910 error(ERR_NONFATAL, "ELF format does not support"
911 " segment base references");
912 } else {
913 if (wrt == NO_SEG) {
914 error(ERR_WARNING | ERR_WARN_GNUELF,
915 "16-bit relocations in ELF is a GNU extension");
916 elf_add_reloc(s, segment, R_386_PC16);
917 } else {
918 error(ERR_NONFATAL,
919 "Unsupported non-32-bit ELF relocation");
922 p = mydata;
923 WRITESHORT(p, *(int64_t *)data - size);
924 elf_sect_write(s, mydata, 2L);
925 } else if (type == OUT_REL4ADR) {
926 if (segment == segto)
927 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
928 if (segment != NO_SEG && segment % 2) {
929 error(ERR_NONFATAL, "ELF format does not support"
930 " segment base references");
931 } else {
932 if (wrt == NO_SEG) {
933 elf_add_reloc(s, segment, R_386_PC32);
934 } else if (wrt == elf_plt_sect + 1) {
935 elf_add_reloc(s, segment, R_386_PLT32);
936 } else if (wrt == elf_gotpc_sect + 1 ||
937 wrt == elf_gotoff_sect + 1 ||
938 wrt == elf_got_sect + 1) {
939 error(ERR_NONFATAL, "ELF format cannot produce PC-"
940 "relative GOT references");
941 } else {
942 error(ERR_NONFATAL, "ELF format does not support this"
943 " use of WRT");
944 wrt = NO_SEG; /* we can at least _try_ to continue */
947 p = mydata;
948 WRITELONG(p, *(int64_t *)data - size);
949 elf_sect_write(s, mydata, 4L);
953 static void elf_write(void)
955 int nsections, align;
956 int scount;
957 char *p;
958 int commlen;
959 char comment[64];
960 int i;
962 struct SAA *symtab;
963 int32_t symtablen, symtablocal;
966 * Work out how many sections we will have. We have SHN_UNDEF,
967 * then the flexible user sections, then the four fixed
968 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
969 * then optionally relocation sections for the user sections.
971 if (of_elf32.current_dfmt == &df_stabs)
972 nsections = 8;
973 else
974 nsections = 5; /* SHN_UNDEF and the fixed ones */
976 add_sectname("", ".comment");
977 add_sectname("", ".shstrtab");
978 add_sectname("", ".symtab");
979 add_sectname("", ".strtab");
980 for (i = 0; i < nsects; i++) {
981 nsections++; /* for the section itself */
982 if (sects[i]->head) {
983 nsections++; /* for its relocations */
984 add_sectname(".rel", sects[i]->name);
988 if (of_elf32.current_dfmt == &df_stabs) {
989 /* in case the debug information is wanted, just add these three sections... */
990 add_sectname("", ".stab");
991 add_sectname("", ".stabstr");
992 add_sectname(".rel", ".stab");
996 * Do the comment.
998 *comment = '\0';
999 commlen =
1000 2 + sprintf(comment + 1, "The Netwide Assembler %s", NASM_VER);
1003 * Output the ELF header.
1005 fwrite("\177ELF\1\1\1", 7, 1, elffp);
1006 fputc(elf_osabi, elffp);
1007 fputc(elf_abiver, elffp);
1008 fwrite("\0\0\0\0\0\0\0", 7, 1, elffp);
1009 fwriteint16_t(1, elffp); /* ET_REL relocatable file */
1010 fwriteint16_t(3, elffp); /* EM_386 processor ID */
1011 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1012 fwriteint32_t(0L, elffp); /* no entry point */
1013 fwriteint32_t(0L, elffp); /* no program header table */
1014 fwriteint32_t(0x40L, elffp); /* section headers straight after
1015 * ELF header plus alignment */
1016 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1017 fwriteint16_t(0x34, elffp); /* size of ELF header */
1018 fwriteint16_t(0, elffp); /* no program header table, again */
1019 fwriteint16_t(0, elffp); /* still no program header table */
1020 fwriteint16_t(0x28, elffp); /* size of section header */
1021 fwriteint16_t(nsections, elffp); /* number of sections */
1022 fwriteint16_t(nsects + 2, elffp); /* string table section index for
1023 * section header table */
1024 fwriteint32_t(0L, elffp); /* align to 0x40 bytes */
1025 fwriteint32_t(0L, elffp);
1026 fwriteint32_t(0L, elffp);
1029 * Build the symbol table and relocation tables.
1031 symtab = elf_build_symtab(&symtablen, &symtablocal);
1032 for (i = 0; i < nsects; i++)
1033 if (sects[i]->head)
1034 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1035 sects[i]->head);
1038 * Now output the section header table.
1041 elf_foffs = 0x40 + 0x28 * nsections;
1042 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1043 elf_foffs += align;
1044 elf_nsect = 0;
1045 elf_sects = nasm_malloc(sizeof(*elf_sects) * (2 * nsects + 10));
1047 elf_section_header(0, 0, 0, NULL, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1048 scount = 1; /* needed for the stabs debugging to track the symtable section */
1049 p = shstrtab + 1;
1050 for (i = 0; i < nsects; i++) {
1051 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1052 (sects[i]->type == SHT_PROGBITS ?
1053 sects[i]->data : NULL), true,
1054 sects[i]->len, 0, 0, sects[i]->align, 0);
1055 p += strlen(p) + 1;
1056 scount++; /* dito */
1058 elf_section_header(p - shstrtab, 1, 0, comment, false, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
1059 scount++; /* dito */
1060 p += strlen(p) + 1;
1061 elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1062 scount++; /* dito */
1063 p += strlen(p) + 1;
1064 elf_section_header(p - shstrtab, 2, 0, symtab, true, symtablen, nsects + 4, symtablocal, 4, 16); /* .symtab */
1065 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1066 p += strlen(p) + 1;
1067 elf_section_header(p - shstrtab, 3, 0, strs, true, strslen, 0, 0, 1, 0); /* .strtab */
1068 for (i = 0; i < nsects; i++)
1069 if (sects[i]->head) {
1070 p += strlen(p) + 1;
1071 elf_section_header(p - shstrtab, 9, 0, sects[i]->rel, true,
1072 sects[i]->rellen, nsects + 3, i + 1, 4, 8);
1074 if (of_elf32.current_dfmt == &df_stabs) {
1075 /* for debugging information, create the last three sections
1076 which are the .stab , .stabstr and .rel.stab sections respectively */
1078 /* this function call creates the stab sections in memory */
1079 stabs32_generate();
1081 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1082 p += strlen(p) + 1;
1083 elf_section_header(p - shstrtab, 1, 0, stabbuf, false, stablen,
1084 nsections - 2, 0, 4, 12);
1086 p += strlen(p) + 1;
1087 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, false,
1088 stabstrlen, 0, 0, 4, 0);
1090 p += strlen(p) + 1;
1091 /* link -> symtable info -> section to refer to */
1092 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, false,
1093 stabrellen, symtabsection, nsections - 3, 4,
1097 fwrite(align_str, align, 1, elffp);
1100 * Now output the sections.
1102 elf_write_sections();
1104 nasm_free(elf_sects);
1105 saa_free(symtab);
1108 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1110 struct SAA *s = saa_init(1L);
1111 struct Symbol *sym;
1112 uint8_t entry[16], *p;
1113 int i;
1115 *len = *local = 0;
1118 * First, an all-zeros entry, required by the ELF spec.
1120 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1121 *len += 16;
1122 (*local)++;
1125 * Next, an entry for the file name.
1127 p = entry;
1128 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1129 WRITELONG(p, 0); /* no value */
1130 WRITELONG(p, 0); /* no size either */
1131 WRITESHORT(p, STT_FILE); /* type FILE */
1132 WRITESHORT(p, SHN_ABS);
1133 saa_wbytes(s, entry, 16L);
1134 *len += 16;
1135 (*local)++;
1138 * Now some standard symbols defining the segments, for relocation
1139 * purposes.
1141 for (i = 1; i <= nsects; i++) {
1142 p = entry;
1143 WRITELONG(p, 0); /* no symbol name */
1144 WRITELONG(p, 0); /* offset zero */
1145 WRITELONG(p, 0); /* size zero */
1146 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1147 WRITESHORT(p, i); /* section id */
1148 saa_wbytes(s, entry, 16L);
1149 *len += 16;
1150 (*local)++;
1154 * Now the other local symbols.
1156 saa_rewind(syms);
1157 while ((sym = saa_rstruct(syms))) {
1158 if (sym->type & SYM_GLOBAL)
1159 continue;
1160 p = entry;
1161 WRITELONG(p, sym->strpos);
1162 WRITELONG(p, sym->value);
1163 WRITELONG(p, sym->size);
1164 WRITECHAR(p, sym->type); /* type and binding */
1165 WRITECHAR(p, sym->other); /* visibility */
1166 WRITESHORT(p, sym->section);
1167 saa_wbytes(s, entry, 16L);
1168 *len += 16;
1169 (*local)++;
1173 * Now the global symbols.
1175 saa_rewind(syms);
1176 while ((sym = saa_rstruct(syms))) {
1177 if (!(sym->type & SYM_GLOBAL))
1178 continue;
1179 p = entry;
1180 WRITELONG(p, sym->strpos);
1181 WRITELONG(p, sym->value);
1182 WRITELONG(p, sym->size);
1183 WRITECHAR(p, sym->type); /* type and binding */
1184 WRITECHAR(p, sym->other); /* visibility */
1185 WRITESHORT(p, sym->section);
1186 saa_wbytes(s, entry, 16L);
1187 *len += 16;
1190 return s;
1193 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1195 struct SAA *s;
1196 uint8_t *p, entry[8];
1198 if (!r)
1199 return NULL;
1201 s = saa_init(1L);
1202 *len = 0;
1204 while (r) {
1205 int32_t sym = r->symbol;
1207 if (sym >= GLOBAL_TEMP_BASE)
1208 sym += -GLOBAL_TEMP_BASE + (nsects + 2) + nlocals;
1210 p = entry;
1211 WRITELONG(p, r->address);
1212 WRITELONG(p, (sym << 8) + r->type);
1213 saa_wbytes(s, entry, 8L);
1214 *len += 8;
1216 r = r->next;
1219 return s;
1222 static void elf_section_header(int name, int type, int flags,
1223 void *data, bool is_saa, int32_t datalen,
1224 int link, int info, int align, int eltsize)
1226 elf_sects[elf_nsect].data = data;
1227 elf_sects[elf_nsect].len = datalen;
1228 elf_sects[elf_nsect].is_saa = is_saa;
1229 elf_nsect++;
1231 fwriteint32_t((int32_t)name, elffp);
1232 fwriteint32_t((int32_t)type, elffp);
1233 fwriteint32_t((int32_t)flags, elffp);
1234 fwriteint32_t(0L, elffp); /* no address, ever, in object files */
1235 fwriteint32_t(type == 0 ? 0L : elf_foffs, elffp);
1236 fwriteint32_t(datalen, elffp);
1237 if (data)
1238 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1239 fwriteint32_t((int32_t)link, elffp);
1240 fwriteint32_t((int32_t)info, elffp);
1241 fwriteint32_t((int32_t)align, elffp);
1242 fwriteint32_t((int32_t)eltsize, elffp);
1245 static void elf_write_sections(void)
1247 int i;
1248 for (i = 0; i < elf_nsect; i++)
1249 if (elf_sects[i].data) {
1250 int32_t len = elf_sects[i].len;
1251 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1252 int32_t align = reallen - len;
1253 if (elf_sects[i].is_saa)
1254 saa_fpwrite(elf_sects[i].data, elffp);
1255 else
1256 fwrite(elf_sects[i].data, len, 1, elffp);
1257 fwrite(align_str, align, 1, elffp);
1261 static void elf_sect_write(struct Section *sect,
1262 const uint8_t *data, uint32_t len)
1264 saa_wbytes(sect->data, data, len);
1265 sect->len += len;
1268 static int32_t elf_segbase(int32_t segment)
1270 return segment;
1273 static int elf_directive(char *directive, char *value, int pass)
1275 bool err;
1276 int64_t n;
1277 char *p;
1279 if (!strcmp(directive, "osabi")) {
1280 if (pass == 2)
1281 return 1; /* ignore in pass 2 */
1283 n = readnum(value, &err);
1284 if (err) {
1285 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1286 return 1;
1288 if (n < 0 || n > 255) {
1289 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1290 return 1;
1292 elf_osabi = n;
1293 elf_abiver = 0;
1295 if ((p = strchr(value,',')) == NULL)
1296 return 1;
1298 n = readnum(p+1, &err);
1299 if (err || n < 0 || n > 255) {
1300 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1301 return 1;
1304 elf_abiver = n;
1305 return 1;
1308 return 0;
1311 static void elf_filename(char *inname, char *outname, efunc error)
1313 strcpy(elf_module, inname);
1314 standard_extension(inname, outname, ".o", error);
1317 static const char *elf_stdmac[] = {
1318 "%define __SECT__ [section .text]",
1319 "%macro __NASM_CDecl__ 1",
1320 "%define $_%1 $%1",
1321 "%endmacro",
1322 "%macro osabi 1+.nolist",
1323 "[osabi %1]",
1324 "%endmacro",
1325 NULL
1327 static int elf_set_info(enum geninfo type, char **val)
1329 (void)type;
1330 (void)val;
1331 return 0;
1334 static struct dfmt df_stabs = {
1335 "ELF32 (i386) stabs debug format for Linux",
1336 "stabs",
1337 stabs32_init,
1338 stabs32_linenum,
1339 stabs32_deflabel,
1340 stabs32_directive,
1341 stabs32_typevalue,
1342 stabs32_output,
1343 stabs32_cleanup
1346 struct dfmt *elf32_debugs_arr[2] = { &df_stabs, NULL };
1348 struct ofmt of_elf32 = {
1349 "ELF32 (i386) object files (e.g. Linux)",
1350 "elf32",
1351 NULL,
1352 elf32_debugs_arr,
1353 &null_debug_form,
1354 elf_stdmac,
1355 elf_init,
1356 elf_set_info,
1357 elf_out,
1358 elf_deflabel,
1359 elf_section_names,
1360 elf_segbase,
1361 elf_directive,
1362 elf_filename,
1363 elf_cleanup
1366 struct ofmt of_elf = {
1367 "ELF (short name for ELF32) ",
1368 "elf",
1369 NULL,
1370 elf32_debugs_arr,
1371 &null_debug_form,
1372 elf_stdmac,
1373 elf_init,
1374 elf_set_info,
1375 elf_out,
1376 elf_deflabel,
1377 elf_section_names,
1378 elf_segbase,
1379 elf_directive,
1380 elf_filename,
1381 elf_cleanup
1383 /* again, the stabs debugging stuff (code) */
1385 void stabs32_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1387 (void)of;
1388 (void)id;
1389 (void)fp;
1390 (void)error;
1393 void stabs32_linenum(const char *filename, int32_t linenumber, int32_t segto)
1395 (void)segto;
1397 if (!stabs_filename) {
1398 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1399 strcpy(stabs_filename, filename);
1400 } else {
1401 if (strcmp(stabs_filename, filename)) {
1402 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1403 in fact, this leak comes in quite handy to maintain a list of files
1404 encountered so far in the symbol lines... */
1406 /* why not nasm_free(stabs_filename); we're done with the old one */
1408 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1409 strcpy(stabs_filename, filename);
1412 stabs_immcall = 1;
1413 currentline = linenumber;
1416 void stabs32_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1417 char *special)
1419 (void)name;
1420 (void)segment;
1421 (void)offset;
1422 (void)is_global;
1423 (void)special;
1426 void stabs32_directive(const char *directive, const char *params)
1428 (void)directive;
1429 (void)params;
1432 void stabs32_typevalue(int32_t type)
1434 int32_t stype, ssize;
1435 switch (TYM_TYPE(type)) {
1436 case TY_LABEL:
1437 ssize = 0;
1438 stype = STT_NOTYPE;
1439 break;
1440 case TY_BYTE:
1441 ssize = 1;
1442 stype = STT_OBJECT;
1443 break;
1444 case TY_WORD:
1445 ssize = 2;
1446 stype = STT_OBJECT;
1447 break;
1448 case TY_DWORD:
1449 ssize = 4;
1450 stype = STT_OBJECT;
1451 break;
1452 case TY_FLOAT:
1453 ssize = 4;
1454 stype = STT_OBJECT;
1455 break;
1456 case TY_QWORD:
1457 ssize = 8;
1458 stype = STT_OBJECT;
1459 break;
1460 case TY_TBYTE:
1461 ssize = 10;
1462 stype = STT_OBJECT;
1463 break;
1464 case TY_OWORD:
1465 ssize = 8;
1466 stype = STT_OBJECT;
1467 break;
1468 case TY_COMMON:
1469 ssize = 0;
1470 stype = STT_COMMON;
1471 break;
1472 case TY_SEG:
1473 ssize = 0;
1474 stype = STT_SECTION;
1475 break;
1476 case TY_EXTERN:
1477 ssize = 0;
1478 stype = STT_NOTYPE;
1479 break;
1480 case TY_EQU:
1481 ssize = 0;
1482 stype = STT_NOTYPE;
1483 break;
1484 default:
1485 ssize = 0;
1486 stype = STT_NOTYPE;
1487 break;
1489 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1490 lastsym->size = ssize;
1491 lastsym->type = stype;
1495 void stabs32_output(int type, void *param)
1497 struct symlininfo *s;
1498 struct linelist *el;
1499 if (type == TY_STABSSYMLIN) {
1500 if (stabs_immcall) {
1501 s = (struct symlininfo *)param;
1502 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1503 return; /* we are only interested in the text stuff */
1504 numlinestabs++;
1505 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1506 el->info.offset = s->offset;
1507 el->info.section = s->section;
1508 el->info.name = s->name;
1509 el->line = currentline;
1510 el->filename = stabs_filename;
1511 el->next = 0;
1512 if (stabslines) {
1513 stabslines->last->next = el;
1514 stabslines->last = el;
1515 } else {
1516 stabslines = el;
1517 stabslines->last = el;
1521 stabs_immcall = 0;
1524 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1525 do {\
1526 WRITELONG(p,n_strx); \
1527 WRITECHAR(p,n_type); \
1528 WRITECHAR(p,n_other); \
1529 WRITESHORT(p,n_desc); \
1530 WRITELONG(p,n_value); \
1531 } while (0)
1533 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1535 void stabs32_generate(void)
1537 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1538 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1539 char **allfiles;
1540 int *fileidx;
1542 struct linelist *ptr;
1544 ptr = stabslines;
1546 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1547 for (i = 0; i < numlinestabs; i++)
1548 allfiles[i] = 0;
1549 numfiles = 0;
1550 while (ptr) {
1551 if (numfiles == 0) {
1552 allfiles[0] = ptr->filename;
1553 numfiles++;
1554 } else {
1555 for (i = 0; i < numfiles; i++) {
1556 if (!strcmp(allfiles[i], ptr->filename))
1557 break;
1559 if (i >= numfiles) {
1560 allfiles[i] = ptr->filename;
1561 numfiles++;
1564 ptr = ptr->next;
1566 strsize = 1;
1567 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1568 for (i = 0; i < numfiles; i++) {
1569 fileidx[i] = strsize;
1570 strsize += strlen(allfiles[i]) + 1;
1572 mainfileindex = 0;
1573 for (i = 0; i < numfiles; i++) {
1574 if (!strcmp(allfiles[i], elf_module)) {
1575 mainfileindex = i;
1576 break;
1580 /* worst case size of the stab buffer would be:
1581 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1583 sbuf =
1584 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1585 sizeof(struct stabentry));
1587 ssbuf = (uint8_t *)nasm_malloc(strsize);
1589 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1590 rptr = rbuf;
1592 for (i = 0; i < numfiles; i++) {
1593 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1595 ssbuf[0] = 0;
1597 stabstrlen = strsize; /* set global variable for length of stab strings */
1599 sptr = sbuf;
1600 ptr = stabslines;
1601 numstabs = 0;
1603 if (ptr) {
1604 /* this is the first stab, its strx points to the filename of the
1605 the source-file, the n_desc field should be set to the number
1606 of remaining stabs
1608 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1610 /* this is the stab for the main source file */
1611 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1613 /* relocation table entry */
1615 /* Since the symbol table has two entries before */
1616 /* the section symbols, the index in the info.section */
1617 /* member must be adjusted by adding 2 */
1619 WRITELONG(rptr, (sptr - sbuf) - 4);
1620 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1622 numstabs++;
1623 currfile = mainfileindex;
1626 while (ptr) {
1627 if (strcmp(allfiles[currfile], ptr->filename)) {
1628 /* oops file has changed... */
1629 for (i = 0; i < numfiles; i++)
1630 if (!strcmp(allfiles[i], ptr->filename))
1631 break;
1632 currfile = i;
1633 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1634 ptr->info.offset);
1635 numstabs++;
1637 /* relocation table entry */
1638 WRITELONG(rptr, (sptr - sbuf) - 4);
1639 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1642 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1643 numstabs++;
1645 /* relocation table entry */
1647 WRITELONG(rptr, (sptr - sbuf) - 4);
1648 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1650 ptr = ptr->next;
1654 ((struct stabentry *)sbuf)->n_desc = numstabs;
1656 nasm_free(allfiles);
1657 nasm_free(fileidx);
1659 stablen = (sptr - sbuf);
1660 stabrellen = (rptr - rbuf);
1661 stabrelbuf = rbuf;
1662 stabbuf = sbuf;
1663 stabstrbuf = ssbuf;
1666 void stabs32_cleanup(void)
1668 struct linelist *ptr, *del;
1669 if (!stabslines)
1670 return;
1671 ptr = stabslines;
1672 while (ptr) {
1673 del = ptr;
1674 ptr = ptr->next;
1675 nasm_free(del);
1677 if (stabbuf)
1678 nasm_free(stabbuf);
1679 if (stabrelbuf)
1680 nasm_free(stabrelbuf);
1681 if (stabstrbuf)
1682 nasm_free(stabstrbuf);
1685 #endif /* OF_ELF */