Provide 64-bit support for ORG directive
[nasm/avx512.git] / output / outelf32.c
blob35d5ac642ddb93d11df0fc6d7b504bd809505ff0
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 licence given in the file "Licence"
7 * distributed in the NASM archive.
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <inttypes.h>
16 #include "nasm.h"
17 #include "nasmlib.h"
18 #include "stdscan.h"
19 #include "outform.h"
21 #ifdef OF_ELF32
24 * Relocation types.
26 enum reloc_type {
27 R_386_32 = 1, /* ordinary absolute relocation */
28 R_386_PC32 = 2, /* PC-relative relocation */
29 R_386_GOT32 = 3, /* an offset into GOT */
30 R_386_PLT32 = 4, /* a PC-relative offset into PLT */
31 R_386_COPY = 5, /* ??? */
32 R_386_GLOB_DAT = 6, /* ??? */
33 R_386_JUMP_SLOT = 7, /* ??? */
34 R_386_RELATIVE = 8, /* ??? */
35 R_386_GOTOFF = 9, /* an offset from GOT base */
36 R_386_GOTPC = 10, /* a PC-relative offset _to_ GOT */
37 /* These are GNU extensions, but useful */
38 R_386_16 = 20, /* A 16-bit absolute relocation */
39 R_386_PC16 = 21, /* A 16-bit PC-relative relocation */
40 R_386_8 = 22, /* An 8-bit absolute relocation */
41 R_386_PC8 = 23 /* An 8-bit PC-relative relocation */
44 struct Reloc {
45 struct Reloc *next;
46 int32_t address; /* relative to _start_ of section */
47 int32_t symbol; /* ELF symbol info thingy */
48 int type; /* type of relocation */
51 struct Symbol {
52 int32_t strpos; /* string table position of name */
53 int32_t section; /* section ID of the symbol */
54 int type; /* symbol type */
55 int other; /* symbol visibility */
56 int32_t value; /* address, or COMMON variable align */
57 int32_t size; /* size of symbol */
58 int32_t globnum; /* symbol table offset if global */
59 struct Symbol *next; /* list of globals in each section */
60 struct Symbol *nextfwd; /* list of unresolved-size symbols */
61 char *name; /* used temporarily if in above list */
64 #define SHT_PROGBITS 1
65 #define SHT_NOBITS 8
67 #define SHF_WRITE 1
68 #define SHF_ALLOC 2
69 #define SHF_EXECINSTR 4
71 struct Section {
72 struct SAA *data;
73 uint32_t len, size, nrelocs;
74 int32_t index;
75 int type; /* SHT_PROGBITS or SHT_NOBITS */
76 int align; /* alignment: power of two */
77 uint32_t flags; /* section flags */
78 char *name;
79 struct SAA *rel;
80 int32_t rellen;
81 struct Reloc *head, **tail;
82 struct Symbol *gsyms; /* global symbols in section */
85 #define SECT_DELTA 32
86 static struct Section **sects;
87 static int nsects, sectlen;
89 #define SHSTR_DELTA 256
90 static char *shstrtab;
91 static int shstrtablen, shstrtabsize;
93 static struct SAA *syms;
94 static uint32_t nlocals, nglobs;
96 static int32_t def_seg;
98 static struct RAA *bsym;
100 static struct SAA *strs;
101 static uint32_t strslen;
103 static FILE *elffp;
104 static efunc error;
105 static evalfunc evaluate;
107 static struct Symbol *fwds;
109 static char elf_module[FILENAME_MAX];
111 extern struct ofmt of_elf32;
112 extern struct ofmt of_elf;
114 #define SHN_ABS 0xFFF1
115 #define SHN_COMMON 0xFFF2
116 #define SHN_UNDEF 0
118 #define SYM_SECTION 0x04
119 #define SYM_GLOBAL 0x10
120 #define SYM_NOTYPE 0x00
121 #define SYM_DATA 0x01
122 #define SYM_FUNCTION 0x02
124 #define STV_DEFAULT 0
125 #define STV_INTERNAL 1
126 #define STV_HIDDEN 2
127 #define STV_PROTECTED 3
129 #define GLOBAL_TEMP_BASE 16 /* bigger than any constant sym id */
131 #define SEG_ALIGN 16 /* alignment of sections in file */
132 #define SEG_ALIGN_1 (SEG_ALIGN-1)
134 static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
136 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
137 static struct ELF_SECTDATA {
138 void *data;
139 int32_t len;
140 int is_saa;
141 } *elf_sects;
142 static int elf_nsect;
143 static int32_t elf_foffs;
145 static void elf_write(void);
146 static void elf_sect_write(struct Section *, const uint8_t *,
147 uint32_t);
148 static void elf_section_header(int, int, int, void *, int, int32_t, int, int,
149 int, int);
150 static void elf_write_sections(void);
151 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
152 static struct SAA *elf_build_reltab(int32_t *, struct Reloc *);
153 static void add_sectname(char *, char *);
155 /* this stuff is needed for the stabs debugging format */
156 #define N_SO 0x64 /* ID for main source file */
157 #define N_SOL 0x84 /* ID for sub-source file */
158 #define N_BINCL 0x82
159 #define N_EINCL 0xA2
160 #define N_SLINE 0x44
161 #define TY_STABSSYMLIN 0x40 /* ouch */
163 struct stabentry {
164 uint32_t n_strx;
165 uint8_t n_type;
166 uint8_t n_other;
167 uint16_t n_desc;
168 uint32_t n_value;
171 struct erel {
172 int offset, info;
175 struct symlininfo {
176 int offset;
177 int section; /* section index */
178 char *name; /* shallow-copied pointer of section name */
181 struct linelist {
182 struct symlininfo info;
183 int line;
184 char *filename;
185 struct linelist *next;
186 struct linelist *last;
189 static struct linelist *stabslines = 0;
190 static int stabs_immcall = 0;
191 static int currentline = 0;
192 static int numlinestabs = 0;
193 static char *stabs_filename = 0;
194 static int symtabsection;
195 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
196 static int stablen, stabstrlen, stabrellen;
198 static struct dfmt df_stabs;
200 void stabs32_init(struct ofmt *, void *, FILE *, efunc);
201 void stabs32_linenum(const char *filename, int32_t linenumber, int32_t);
202 void stabs32_deflabel(char *, int32_t, int32_t, int, char *);
203 void stabs32_directive(const char *, const char *);
204 void stabs32_typevalue(int32_t);
205 void stabs32_output(int, void *);
206 void stabs32_generate();
207 void stabs32_cleanup();
209 /* end of stabs debugging stuff */
212 * Special section numbers which are used to define ELF special
213 * symbols, which can be used with WRT to provide PIC relocation
214 * types.
216 static int32_t elf_gotpc_sect, elf_gotoff_sect;
217 static int32_t elf_got_sect, elf_plt_sect;
218 static int32_t elf_sym_sect;
220 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
222 elffp = fp;
223 error = errfunc;
224 evaluate = eval;
225 (void)ldef; /* placate optimisers */
226 sects = NULL;
227 nsects = sectlen = 0;
228 syms = saa_init((int32_t)sizeof(struct Symbol));
229 nlocals = nglobs = 0;
230 bsym = raa_init();
231 strs = saa_init(1L);
232 saa_wbytes(strs, "\0", 1L);
233 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
234 strslen = 2 + strlen(elf_module);
235 shstrtab = NULL;
236 shstrtablen = shstrtabsize = 0;;
237 add_sectname("", "");
239 fwds = NULL;
241 elf_gotpc_sect = seg_alloc();
242 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf32,
243 error);
244 elf_gotoff_sect = seg_alloc();
245 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf32,
246 error);
247 elf_got_sect = seg_alloc();
248 ldef("..got", elf_got_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf32,
249 error);
250 elf_plt_sect = seg_alloc();
251 ldef("..plt", elf_plt_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf32,
252 error);
253 elf_sym_sect = seg_alloc();
254 ldef("..sym", elf_sym_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf32,
255 error);
257 def_seg = seg_alloc();
260 static void elf_cleanup(int debuginfo)
262 struct Reloc *r;
263 int i;
265 (void)debuginfo;
267 elf_write();
268 fclose(elffp);
269 for (i = 0; i < nsects; i++) {
270 if (sects[i]->type != SHT_NOBITS)
271 saa_free(sects[i]->data);
272 if (sects[i]->head)
273 saa_free(sects[i]->rel);
274 while (sects[i]->head) {
275 r = sects[i]->head;
276 sects[i]->head = sects[i]->head->next;
277 nasm_free(r);
280 nasm_free(sects);
281 saa_free(syms);
282 raa_free(bsym);
283 saa_free(strs);
284 if (of_elf32.current_dfmt) {
285 of_elf32.current_dfmt->cleanup();
289 static void add_sectname(char *firsthalf, char *secondhalf)
291 int len = strlen(firsthalf) + strlen(secondhalf);
292 while (shstrtablen + len + 1 > shstrtabsize)
293 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
294 strcpy(shstrtab + shstrtablen, firsthalf);
295 strcat(shstrtab + shstrtablen, secondhalf);
296 shstrtablen += len + 1;
299 static int elf_make_section(char *name, int type, int flags, int align)
301 struct Section *s;
303 s = nasm_malloc(sizeof(*s));
305 if (type != SHT_NOBITS)
306 s->data = saa_init(1L);
307 s->head = NULL;
308 s->tail = &s->head;
309 s->len = s->size = 0;
310 s->nrelocs = 0;
311 if (!strcmp(name, ".text"))
312 s->index = def_seg;
313 else
314 s->index = seg_alloc();
315 add_sectname("", name);
316 s->name = nasm_malloc(1 + strlen(name));
317 strcpy(s->name, name);
318 s->type = type;
319 s->flags = flags;
320 s->align = align;
321 s->gsyms = NULL;
323 if (nsects >= sectlen)
324 sects =
325 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
326 sects[nsects++] = s;
328 return nsects - 1;
331 static int32_t elf_section_names(char *name, int pass, int *bits)
333 char *p;
334 int flags_and, flags_or, type, align, i;
337 * Default is 32 bits.
339 if (!name) {
340 *bits = 32;
341 return def_seg;
344 p = name;
345 while (*p && !isspace(*p))
346 p++;
347 if (*p)
348 *p++ = '\0';
349 flags_and = flags_or = type = align = 0;
351 while (*p && isspace(*p))
352 p++;
353 while (*p) {
354 char *q = p;
355 while (*p && !isspace(*p))
356 p++;
357 if (*p)
358 *p++ = '\0';
359 while (*p && isspace(*p))
360 p++;
362 if (!nasm_strnicmp(q, "align=", 6)) {
363 align = atoi(q + 6);
364 if (align == 0)
365 align = 1;
366 if ((align - 1) & align) { /* means it's not a power of two */
367 error(ERR_NONFATAL, "section alignment %d is not"
368 " a power of two", align);
369 align = 1;
371 } else if (!nasm_stricmp(q, "alloc")) {
372 flags_and |= SHF_ALLOC;
373 flags_or |= SHF_ALLOC;
374 } else if (!nasm_stricmp(q, "noalloc")) {
375 flags_and |= SHF_ALLOC;
376 flags_or &= ~SHF_ALLOC;
377 } else if (!nasm_stricmp(q, "exec")) {
378 flags_and |= SHF_EXECINSTR;
379 flags_or |= SHF_EXECINSTR;
380 } else if (!nasm_stricmp(q, "noexec")) {
381 flags_and |= SHF_EXECINSTR;
382 flags_or &= ~SHF_EXECINSTR;
383 } else if (!nasm_stricmp(q, "write")) {
384 flags_and |= SHF_WRITE;
385 flags_or |= SHF_WRITE;
386 } else if (!nasm_stricmp(q, "nowrite")) {
387 flags_and |= SHF_WRITE;
388 flags_or &= ~SHF_WRITE;
389 } else if (!nasm_stricmp(q, "progbits")) {
390 type = SHT_PROGBITS;
391 } else if (!nasm_stricmp(q, "nobits")) {
392 type = SHT_NOBITS;
396 if (!strcmp(name, ".comment") ||
397 !strcmp(name, ".shstrtab") ||
398 !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
399 error(ERR_NONFATAL, "attempt to redefine reserved section"
400 "name `%s'", name);
401 return NO_SEG;
404 for (i = 0; i < nsects; i++)
405 if (!strcmp(name, sects[i]->name))
406 break;
407 if (i == nsects) {
408 if (!strcmp(name, ".text"))
409 i = elf_make_section(name, SHT_PROGBITS,
410 SHF_ALLOC | SHF_EXECINSTR, 16);
411 else if (!strcmp(name, ".rodata"))
412 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
413 else if (!strcmp(name, ".data"))
414 i = elf_make_section(name, SHT_PROGBITS,
415 SHF_ALLOC | SHF_WRITE, 4);
416 else if (!strcmp(name, ".bss"))
417 i = elf_make_section(name, SHT_NOBITS,
418 SHF_ALLOC | SHF_WRITE, 4);
419 else
420 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
421 if (type)
422 sects[i]->type = type;
423 if (align)
424 sects[i]->align = align;
425 sects[i]->flags &= ~flags_and;
426 sects[i]->flags |= flags_or;
427 } else if (pass == 1) {
428 if (type || align || flags_and)
429 error(ERR_WARNING, "section attributes ignored on"
430 " redeclaration of section `%s'", name);
433 return sects[i]->index;
436 static void elf_deflabel(char *name, int32_t segment, int32_t offset,
437 int is_global, char *special)
439 int pos = strslen;
440 struct Symbol *sym;
441 int special_used = FALSE;
443 #if defined(DEBUG) && DEBUG>2
444 fprintf(stderr,
445 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
446 name, segment, offset, is_global, special);
447 #endif
448 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
450 * This is a NASM special symbol. We never allow it into
451 * the ELF symbol table, even if it's a valid one. If it
452 * _isn't_ a valid one, we should barf immediately.
454 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
455 strcmp(name, "..got") && strcmp(name, "..plt") &&
456 strcmp(name, "..sym"))
457 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
458 return;
461 if (is_global == 3) {
462 struct Symbol **s;
464 * Fix up a forward-reference symbol size from the first
465 * pass.
467 for (s = &fwds; *s; s = &(*s)->nextfwd)
468 if (!strcmp((*s)->name, name)) {
469 struct tokenval tokval;
470 expr *e;
471 char *p = special;
473 while (*p && !isspace(*p))
474 p++;
475 while (*p && isspace(*p))
476 p++;
477 stdscan_reset();
478 stdscan_bufptr = p;
479 tokval.t_type = TOKEN_INVALID;
480 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
481 if (e) {
482 if (!is_simple(e))
483 error(ERR_NONFATAL, "cannot use relocatable"
484 " expression as symbol size");
485 else
486 (*s)->size = reloc_value(e);
490 * Remove it from the list of unresolved sizes.
492 nasm_free((*s)->name);
493 *s = (*s)->nextfwd;
494 return;
496 return; /* it wasn't an important one */
499 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
500 strslen += 1 + strlen(name);
502 sym = saa_wstruct(syms);
504 sym->strpos = pos;
505 sym->type = is_global ? SYM_GLOBAL : 0;
506 sym->other = STV_DEFAULT;
507 sym->size = 0;
508 if (segment == NO_SEG)
509 sym->section = SHN_ABS;
510 else {
511 int i;
512 sym->section = SHN_UNDEF;
513 if (nsects == 0 && segment == def_seg) {
514 int tempint;
515 if (segment != elf_section_names(".text", 2, &tempint))
516 error(ERR_PANIC,
517 "strange segment conditions in ELF driver");
518 sym->section = nsects;
519 } else {
520 for (i = 0; i < nsects; i++)
521 if (segment == sects[i]->index) {
522 sym->section = i + 1;
523 break;
528 if (is_global == 2) {
529 sym->size = offset;
530 sym->value = 0;
531 sym->section = SHN_COMMON;
533 * We have a common variable. Check the special text to see
534 * if it's a valid number and power of two; if so, store it
535 * as the alignment for the common variable.
537 if (special) {
538 int err;
539 sym->value = readnum(special, &err);
540 if (err)
541 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
542 " valid number", special);
543 else if ((sym->value | (sym->value - 1)) != 2 * sym->value - 1)
544 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
545 " power of two", special);
547 special_used = TRUE;
548 } else
549 sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
551 if (sym->type == SYM_GLOBAL) {
553 * If sym->section == SHN_ABS, then the first line of the
554 * else section would cause a core dump, because its a reference
555 * beyond the end of the section array.
556 * This behaviour is exhibited by this code:
557 * GLOBAL crash_nasm
558 * crash_nasm equ 0
559 * To avoid such a crash, such requests are silently discarded.
560 * This may not be the best solution.
562 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
563 bsym = raa_write(bsym, segment, nglobs);
564 } else if (sym->section != SHN_ABS) {
566 * This is a global symbol; so we must add it to the linked
567 * list of global symbols in its section. We'll push it on
568 * the beginning of the list, because it doesn't matter
569 * much which end we put it on and it's easier like this.
571 * In addition, we check the special text for symbol
572 * type and size information.
574 sym->next = sects[sym->section - 1]->gsyms;
575 sects[sym->section - 1]->gsyms = sym;
577 if (special) {
578 int n = strcspn(special, " \t");
580 if (!nasm_strnicmp(special, "function", n))
581 sym->type |= SYM_FUNCTION;
582 else if (!nasm_strnicmp(special, "data", n) ||
583 !nasm_strnicmp(special, "object", n))
584 sym->type |= SYM_DATA;
585 else if (!nasm_strnicmp(special, "notype", n))
586 sym->type |= SYM_NOTYPE;
587 else
588 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
589 n, special);
590 special += n;
592 while (isspace(*special))
593 ++special;
594 if (*special) {
595 n = strcspn(special, " \t");
596 if (!nasm_strnicmp(special, "default", n))
597 sym->other = STV_DEFAULT;
598 else if (!nasm_strnicmp(special, "internal", n))
599 sym->other = STV_INTERNAL;
600 else if (!nasm_strnicmp(special, "hidden", n))
601 sym->other = STV_HIDDEN;
602 else if (!nasm_strnicmp(special, "protected", n))
603 sym->other = STV_PROTECTED;
604 else
605 n = 0;
606 special += n;
609 if (*special) {
610 struct tokenval tokval;
611 expr *e;
612 int fwd = FALSE;
613 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
615 while (special[n] && isspace(special[n]))
616 n++;
618 * We have a size expression; attempt to
619 * evaluate it.
621 stdscan_reset();
622 stdscan_bufptr = special + n;
623 tokval.t_type = TOKEN_INVALID;
624 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
625 NULL);
626 if (fwd) {
627 sym->nextfwd = fwds;
628 fwds = sym;
629 sym->name = nasm_strdup(name);
630 } else if (e) {
631 if (!is_simple(e))
632 error(ERR_NONFATAL, "cannot use relocatable"
633 " expression as symbol size");
634 else
635 sym->size = reloc_value(e);
637 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
639 special_used = TRUE;
642 sym->globnum = nglobs;
643 nglobs++;
644 } else
645 nlocals++;
647 if (special && !special_used)
648 error(ERR_NONFATAL, "no special symbol features supported here");
651 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
653 struct Reloc *r;
655 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
656 sect->tail = &r->next;
657 r->next = NULL;
659 r->address = sect->len;
660 if (segment == NO_SEG)
661 r->symbol = 2;
662 else {
663 int i;
664 r->symbol = 0;
665 for (i = 0; i < nsects; i++)
666 if (segment == sects[i]->index)
667 r->symbol = i + 3;
668 if (!r->symbol)
669 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
671 r->type = type;
673 sect->nrelocs++;
677 * This routine deals with ..got and ..sym relocations: the more
678 * complicated kinds. In shared-library writing, some relocations
679 * with respect to global symbols must refer to the precise symbol
680 * rather than referring to an offset from the base of the section
681 * _containing_ the symbol. Such relocations call to this routine,
682 * which searches the symbol list for the symbol in question.
684 * R_386_GOT32 references require the _exact_ symbol address to be
685 * used; R_386_32 references can be at an offset from the symbol.
686 * The boolean argument `exact' tells us this.
688 * Return value is the adjusted value of `addr', having become an
689 * offset from the symbol rather than the section. Should always be
690 * zero when returning from an exact call.
692 * Limitation: if you define two symbols at the same place,
693 * confusion will occur.
695 * Inefficiency: we search, currently, using a linked list which
696 * isn't even necessarily sorted.
698 static int32_t elf_add_gsym_reloc(struct Section *sect,
699 int32_t segment, int32_t offset,
700 int type, int exact)
702 struct Reloc *r;
703 struct Section *s;
704 struct Symbol *sym, *sm;
705 int i;
708 * First look up the segment/offset pair and find a global
709 * symbol corresponding to it. If it's not one of our segments,
710 * then it must be an external symbol, in which case we're fine
711 * doing a normal elf_add_reloc after first sanity-checking
712 * that the offset from the symbol is zero.
714 s = NULL;
715 for (i = 0; i < nsects; i++)
716 if (segment == sects[i]->index) {
717 s = sects[i];
718 break;
720 if (!s) {
721 if (exact && offset != 0)
722 error(ERR_NONFATAL, "unable to find a suitable global symbol"
723 " for this reference");
724 else
725 elf_add_reloc(sect, segment, type);
726 return offset;
729 if (exact) {
731 * Find a symbol pointing _exactly_ at this one.
733 for (sym = s->gsyms; sym; sym = sym->next)
734 if (sym->value == offset)
735 break;
736 } else {
738 * Find the nearest symbol below this one.
740 sym = NULL;
741 for (sm = s->gsyms; sm; sm = sm->next)
742 if (sm->value <= offset && (!sym || sm->value > sym->value))
743 sym = sm;
745 if (!sym && exact) {
746 error(ERR_NONFATAL, "unable to find a suitable global symbol"
747 " for this reference");
748 return 0;
751 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
752 sect->tail = &r->next;
753 r->next = NULL;
755 r->address = sect->len;
756 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
757 r->type = type;
759 sect->nrelocs++;
761 return offset - sym->value;
764 static void elf_out(int32_t segto, const void *data, uint32_t type,
765 int32_t segment, int32_t wrt)
767 struct Section *s;
768 int32_t realbytes = type & OUT_SIZMASK;
769 int32_t addr;
770 uint8_t mydata[4], *p;
771 int i;
772 static struct symlininfo sinfo;
774 type &= OUT_TYPMASK;
777 * handle absolute-assembly (structure definitions)
779 if (segto == NO_SEG) {
780 if (type != OUT_RESERVE)
781 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
782 " space");
783 return;
786 s = NULL;
787 for (i = 0; i < nsects; i++)
788 if (segto == sects[i]->index) {
789 s = sects[i];
790 break;
792 if (!s) {
793 int tempint; /* ignored */
794 if (segto != elf_section_names(".text", 2, &tempint))
795 error(ERR_PANIC, "strange segment conditions in ELF driver");
796 else {
797 s = sects[nsects - 1];
798 i = nsects - 1;
802 /* again some stabs debugging stuff */
803 if (of_elf32.current_dfmt) {
804 sinfo.offset = s->len;
805 sinfo.section = i;
806 sinfo.name = s->name;
807 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
809 /* end of debugging stuff */
811 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
812 error(ERR_WARNING, "attempt to initialize memory in"
813 " BSS section `%s': ignored", s->name);
814 if (type == OUT_REL2ADR)
815 realbytes = 2;
816 else if (type == OUT_REL4ADR)
817 realbytes = 4;
818 s->len += realbytes;
819 return;
822 if (type == OUT_RESERVE) {
823 if (s->type == SHT_PROGBITS) {
824 error(ERR_WARNING, "uninitialized space declared in"
825 " non-BSS section `%s': zeroing", s->name);
826 elf_sect_write(s, NULL, realbytes);
827 } else
828 s->len += realbytes;
829 } else if (type == OUT_RAWDATA) {
830 if (segment != NO_SEG)
831 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
832 elf_sect_write(s, data, realbytes);
833 } else if (type == OUT_ADDRESS) {
834 int gnu16 = 0;
835 addr = *(int32_t *)data;
836 if (segment != NO_SEG) {
837 if (segment % 2) {
838 error(ERR_NONFATAL, "ELF format does not support"
839 " segment base references");
840 } else {
841 if (wrt == NO_SEG) {
842 if (realbytes == 2) {
843 gnu16 = 1;
844 elf_add_reloc(s, segment, R_386_16);
845 } else {
846 elf_add_reloc(s, segment, R_386_32);
848 } else if (wrt == elf_gotpc_sect + 1) {
850 * The user will supply GOT relative to $$. ELF
851 * will let us have GOT relative to $. So we
852 * need to fix up the data item by $-$$.
854 addr += s->len;
855 elf_add_reloc(s, segment, R_386_GOTPC);
856 } else if (wrt == elf_gotoff_sect + 1) {
857 elf_add_reloc(s, segment, R_386_GOTOFF);
858 } else if (wrt == elf_got_sect + 1) {
859 addr = elf_add_gsym_reloc(s, segment, addr,
860 R_386_GOT32, TRUE);
861 } else if (wrt == elf_sym_sect + 1) {
862 if (realbytes == 2) {
863 gnu16 = 1;
864 addr = elf_add_gsym_reloc(s, segment, addr,
865 R_386_16, FALSE);
866 } else {
867 addr = elf_add_gsym_reloc(s, segment, addr,
868 R_386_32, FALSE);
870 } else if (wrt == elf_plt_sect + 1) {
871 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
872 "relative PLT references");
873 } else {
874 error(ERR_NONFATAL, "ELF format does not support this"
875 " use of WRT");
876 wrt = NO_SEG; /* we can at least _try_ to continue */
880 p = mydata;
881 if (gnu16) {
882 error(ERR_WARNING | ERR_WARN_GNUELF,
883 "16-bit relocations in ELF is a GNU extension");
884 WRITESHORT(p, addr);
885 } else {
886 if (realbytes != 4 && segment != NO_SEG) {
887 error(ERR_NONFATAL,
888 "Unsupported non-32-bit ELF relocation");
890 WRITELONG(p, addr);
892 elf_sect_write(s, mydata, realbytes);
893 } else if (type == OUT_REL2ADR) {
894 if (segment == segto)
895 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
896 if (segment != NO_SEG && segment % 2) {
897 error(ERR_NONFATAL, "ELF format does not support"
898 " segment base references");
899 } else {
900 if (wrt == NO_SEG) {
901 error(ERR_WARNING | ERR_WARN_GNUELF,
902 "16-bit relocations in ELF is a GNU extension");
903 elf_add_reloc(s, segment, R_386_PC16);
904 } else {
905 error(ERR_NONFATAL,
906 "Unsupported non-32-bit ELF relocation");
909 p = mydata;
910 WRITESHORT(p, *(int32_t *)data - realbytes);
911 elf_sect_write(s, mydata, 2L);
912 } else if (type == OUT_REL4ADR) {
913 if (segment == segto)
914 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
915 if (segment != NO_SEG && segment % 2) {
916 error(ERR_NONFATAL, "ELF format does not support"
917 " segment base references");
918 } else {
919 if (wrt == NO_SEG) {
920 elf_add_reloc(s, segment, R_386_PC32);
921 } else if (wrt == elf_plt_sect + 1) {
922 elf_add_reloc(s, segment, R_386_PLT32);
923 } else if (wrt == elf_gotpc_sect + 1 ||
924 wrt == elf_gotoff_sect + 1 ||
925 wrt == elf_got_sect + 1) {
926 error(ERR_NONFATAL, "ELF format cannot produce PC-"
927 "relative GOT references");
928 } else {
929 error(ERR_NONFATAL, "ELF format does not support this"
930 " use of WRT");
931 wrt = NO_SEG; /* we can at least _try_ to continue */
934 p = mydata;
935 WRITELONG(p, *(int32_t *)data - realbytes);
936 elf_sect_write(s, mydata, 4L);
940 static void elf_write(void)
942 int nsections, align;
943 int scount;
944 char *p;
945 int commlen;
946 char comment[64];
947 int i;
949 struct SAA *symtab;
950 int32_t symtablen, symtablocal;
953 * Work out how many sections we will have. We have SHN_UNDEF,
954 * then the flexible user sections, then the four fixed
955 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
956 * then optionally relocation sections for the user sections.
958 if (of_elf32.current_dfmt == &df_stabs)
959 nsections = 8;
960 else
961 nsections = 5; /* SHN_UNDEF and the fixed ones */
963 add_sectname("", ".comment");
964 add_sectname("", ".shstrtab");
965 add_sectname("", ".symtab");
966 add_sectname("", ".strtab");
967 for (i = 0; i < nsects; i++) {
968 nsections++; /* for the section itself */
969 if (sects[i]->head) {
970 nsections++; /* for its relocations */
971 add_sectname(".rel", sects[i]->name);
975 if (of_elf32.current_dfmt == &df_stabs) {
976 /* in case the debug information is wanted, just add these three sections... */
977 add_sectname("", ".stab");
978 add_sectname("", ".stabstr");
979 add_sectname(".rel", ".stab");
983 * Do the comment.
985 *comment = '\0';
986 commlen =
987 2 + sprintf(comment + 1, "The Netwide Assembler %s", NASM_VER);
990 * Output the ELF header.
992 fwrite("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp);
993 fwriteint16_t(1, elffp); /* ET_REL relocatable file */
994 fwriteint16_t(3, elffp); /* EM_386 processor ID */
995 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
996 fwriteint32_t(0L, elffp); /* no entry point */
997 fwriteint32_t(0L, elffp); /* no program header table */
998 fwriteint32_t(0x40L, elffp); /* section headers straight after
999 * ELF header plus alignment */
1000 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1001 fwriteint16_t(0x34, elffp); /* size of ELF header */
1002 fwriteint16_t(0, elffp); /* no program header table, again */
1003 fwriteint16_t(0, elffp); /* still no program header table */
1004 fwriteint16_t(0x28, elffp); /* size of section header */
1005 fwriteint16_t(nsections, elffp); /* number of sections */
1006 fwriteint16_t(nsects + 2, elffp); /* string table section index for
1007 * section header table */
1008 fwriteint32_t(0L, elffp); /* align to 0x40 bytes */
1009 fwriteint32_t(0L, elffp);
1010 fwriteint32_t(0L, elffp);
1013 * Build the symbol table and relocation tables.
1015 symtab = elf_build_symtab(&symtablen, &symtablocal);
1016 for (i = 0; i < nsects; i++)
1017 if (sects[i]->head)
1018 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1019 sects[i]->head);
1022 * Now output the section header table.
1025 elf_foffs = 0x40 + 0x28 * nsections;
1026 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1027 elf_foffs += align;
1028 elf_nsect = 0;
1029 elf_sects = nasm_malloc(sizeof(*elf_sects) * (2 * nsects + 10));
1031 elf_section_header(0, 0, 0, NULL, FALSE, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1032 scount = 1; /* needed for the stabs debugging to track the symtable section */
1033 p = shstrtab + 1;
1034 for (i = 0; i < nsects; i++) {
1035 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1036 (sects[i]->type == SHT_PROGBITS ?
1037 sects[i]->data : NULL), TRUE,
1038 sects[i]->len, 0, 0, sects[i]->align, 0);
1039 p += strlen(p) + 1;
1040 scount++; /* dito */
1042 elf_section_header(p - shstrtab, 1, 0, comment, FALSE, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
1043 scount++; /* dito */
1044 p += strlen(p) + 1;
1045 elf_section_header(p - shstrtab, 3, 0, shstrtab, FALSE, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1046 scount++; /* dito */
1047 p += strlen(p) + 1;
1048 elf_section_header(p - shstrtab, 2, 0, symtab, TRUE, symtablen, nsects + 4, symtablocal, 4, 16); /* .symtab */
1049 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1050 p += strlen(p) + 1;
1051 elf_section_header(p - shstrtab, 3, 0, strs, TRUE, strslen, 0, 0, 1, 0); /* .strtab */
1052 for (i = 0; i < nsects; i++)
1053 if (sects[i]->head) {
1054 p += strlen(p) + 1;
1055 elf_section_header(p - shstrtab, 9, 0, sects[i]->rel, TRUE,
1056 sects[i]->rellen, nsects + 3, i + 1, 4, 8);
1058 if (of_elf32.current_dfmt == &df_stabs) {
1059 /* for debugging information, create the last three sections
1060 which are the .stab , .stabstr and .rel.stab sections respectively */
1062 /* this function call creates the stab sections in memory */
1063 stabs32_generate();
1065 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1066 p += strlen(p) + 1;
1067 elf_section_header(p - shstrtab, 1, 0, stabbuf, 0, stablen,
1068 nsections - 2, 0, 4, 12);
1070 p += strlen(p) + 1;
1071 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, 0,
1072 stabstrlen, 0, 0, 4, 0);
1074 p += strlen(p) + 1;
1075 /* link -> symtable info -> section to refer to */
1076 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, 0,
1077 stabrellen, symtabsection, nsections - 3, 4,
1081 fwrite(align_str, align, 1, elffp);
1084 * Now output the sections.
1086 elf_write_sections();
1088 nasm_free(elf_sects);
1089 saa_free(symtab);
1092 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1094 struct SAA *s = saa_init(1L);
1095 struct Symbol *sym;
1096 uint8_t entry[16], *p;
1097 int i;
1099 *len = *local = 0;
1102 * First, an all-zeros entry, required by the ELF spec.
1104 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1105 *len += 16;
1106 (*local)++;
1109 * Next, an entry for the file name.
1111 p = entry;
1112 WRITELONG(p, 1); /* we know it's 1st thing in strtab */
1113 WRITELONG(p, 0); /* no value */
1114 WRITELONG(p, 0); /* no size either */
1115 WRITESHORT(p, 4); /* type FILE */
1116 WRITESHORT(p, SHN_ABS);
1117 saa_wbytes(s, entry, 16L);
1118 *len += 16;
1119 (*local)++;
1122 * Now some standard symbols defining the segments, for relocation
1123 * purposes.
1125 for (i = 1; i <= nsects + 1; i++) {
1126 p = entry;
1127 WRITELONG(p, 0); /* no symbol name */
1128 WRITELONG(p, 0); /* offset zero */
1129 WRITELONG(p, 0); /* size zero */
1130 WRITESHORT(p, 3); /* local section-type thing */
1131 WRITESHORT(p, (i == 1 ? SHN_ABS : i - 1)); /* the section id */
1132 saa_wbytes(s, entry, 16L);
1133 *len += 16;
1134 (*local)++;
1138 * Now the other local symbols.
1140 saa_rewind(syms);
1141 while ((sym = saa_rstruct(syms))) {
1142 if (sym->type & SYM_GLOBAL)
1143 continue;
1144 p = entry;
1145 WRITELONG(p, sym->strpos);
1146 WRITELONG(p, sym->value);
1147 WRITELONG(p, sym->size);
1148 WRITECHAR(p, sym->type); /* local non-typed thing */
1149 WRITECHAR(p, sym->other);
1150 WRITESHORT(p, sym->section);
1151 saa_wbytes(s, entry, 16L);
1152 *len += 16;
1153 (*local)++;
1157 * Now the global symbols.
1159 saa_rewind(syms);
1160 while ((sym = saa_rstruct(syms))) {
1161 if (!(sym->type & SYM_GLOBAL))
1162 continue;
1163 p = entry;
1164 WRITELONG(p, sym->strpos);
1165 WRITELONG(p, sym->value);
1166 WRITELONG(p, sym->size);
1167 WRITECHAR(p, sym->type); /* global non-typed thing */
1168 WRITECHAR(p, sym->other);
1169 WRITESHORT(p, sym->section);
1170 saa_wbytes(s, entry, 16L);
1171 *len += 16;
1174 return s;
1177 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1179 struct SAA *s;
1180 uint8_t *p, entry[8];
1182 if (!r)
1183 return NULL;
1185 s = saa_init(1L);
1186 *len = 0;
1188 while (r) {
1189 int32_t sym = r->symbol;
1191 if (sym >= GLOBAL_TEMP_BASE)
1192 sym += -GLOBAL_TEMP_BASE + (nsects + 3) + nlocals;
1194 p = entry;
1195 WRITELONG(p, r->address);
1196 WRITELONG(p, (sym << 8) + r->type);
1197 saa_wbytes(s, entry, 8L);
1198 *len += 8;
1200 r = r->next;
1203 return s;
1206 static void elf_section_header(int name, int type, int flags,
1207 void *data, int is_saa, int32_t datalen,
1208 int link, int info, int align, int eltsize)
1210 elf_sects[elf_nsect].data = data;
1211 elf_sects[elf_nsect].len = datalen;
1212 elf_sects[elf_nsect].is_saa = is_saa;
1213 elf_nsect++;
1215 fwriteint32_t((int32_t)name, elffp);
1216 fwriteint32_t((int32_t)type, elffp);
1217 fwriteint32_t((int32_t)flags, elffp);
1218 fwriteint32_t(0L, elffp); /* no address, ever, in object files */
1219 fwriteint32_t(type == 0 ? 0L : elf_foffs, elffp);
1220 fwriteint32_t(datalen, elffp);
1221 if (data)
1222 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1223 fwriteint32_t((int32_t)link, elffp);
1224 fwriteint32_t((int32_t)info, elffp);
1225 fwriteint32_t((int32_t)align, elffp);
1226 fwriteint32_t((int32_t)eltsize, elffp);
1229 static void elf_write_sections(void)
1231 int i;
1232 for (i = 0; i < elf_nsect; i++)
1233 if (elf_sects[i].data) {
1234 int32_t len = elf_sects[i].len;
1235 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1236 int32_t align = reallen - len;
1237 if (elf_sects[i].is_saa)
1238 saa_fpwrite(elf_sects[i].data, elffp);
1239 else
1240 fwrite(elf_sects[i].data, len, 1, elffp);
1241 fwrite(align_str, align, 1, elffp);
1245 static void elf_sect_write(struct Section *sect,
1246 const uint8_t *data, uint32_t len)
1248 saa_wbytes(sect->data, data, len);
1249 sect->len += len;
1252 static int32_t elf_segbase(int32_t segment)
1254 return segment;
1257 static int elf_directive(char *directive, char *value, int pass)
1259 (void)directive;
1260 (void)value;
1261 (void)pass;
1262 return 0;
1265 static void elf_filename(char *inname, char *outname, efunc error)
1267 strcpy(elf_module, inname);
1268 standard_extension(inname, outname, ".o", error);
1271 static const char *elf_stdmac[] = {
1272 "%define __SECT__ [section .text]",
1273 "%macro __NASM_CDecl__ 1",
1274 "%define $_%1 $%1",
1275 "%endmacro",
1276 NULL
1278 static int elf_set_info(enum geninfo type, char **val)
1280 (void)type;
1281 (void)val;
1282 return 0;
1285 static struct dfmt df_stabs = {
1286 "ELF32 (i386) stabs debug format for Linux",
1287 "stabs",
1288 stabs32_init,
1289 stabs32_linenum,
1290 stabs32_deflabel,
1291 stabs32_directive,
1292 stabs32_typevalue,
1293 stabs32_output,
1294 stabs32_cleanup
1297 struct dfmt *elf32_debugs_arr[2] = { &df_stabs, NULL };
1299 struct ofmt of_elf32 = {
1300 "ELF32 (i386) object files (e.g. Linux)",
1301 "elf32",
1302 NULL,
1303 elf32_debugs_arr,
1304 &null_debug_form,
1305 elf_stdmac,
1306 elf_init,
1307 elf_set_info,
1308 elf_out,
1309 elf_deflabel,
1310 elf_section_names,
1311 elf_segbase,
1312 elf_directive,
1313 elf_filename,
1314 elf_cleanup
1317 struct ofmt of_elf = {
1318 "ELF (short name for ELF32) ",
1319 "elf",
1320 NULL,
1321 elf32_debugs_arr,
1322 &null_debug_form,
1323 elf_stdmac,
1324 elf_init,
1325 elf_set_info,
1326 elf_out,
1327 elf_deflabel,
1328 elf_section_names,
1329 elf_segbase,
1330 elf_directive,
1331 elf_filename,
1332 elf_cleanup
1334 /* again, the stabs debugging stuff (code) */
1336 void stabs32_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1338 (void)of;
1339 (void)id;
1340 (void)fp;
1341 (void)error;
1344 void stabs32_linenum(const char *filename, int32_t linenumber, int32_t segto)
1346 (void)segto;
1347 if (!stabs_filename) {
1348 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1349 strcpy(stabs_filename, filename);
1350 } else {
1351 if (strcmp(stabs_filename, filename)) {
1352 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1353 in fact, this leak comes in quite handy to maintain a list of files
1354 encountered so far in the symbol lines... */
1356 /* why not nasm_free(stabs_filename); we're done with the old one */
1358 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1359 strcpy(stabs_filename, filename);
1362 stabs_immcall = 1;
1363 currentline = linenumber;
1366 void stabs32_deflabel(char *name, int32_t segment, int32_t offset, int is_global,
1367 char *special)
1369 (void)name;
1370 (void)segment;
1371 (void)offset;
1372 (void)is_global;
1373 (void)special;
1376 void stabs32_directive(const char *directive, const char *params)
1378 (void)directive;
1379 (void)params;
1382 void stabs32_typevalue(int32_t type)
1384 (void)type;
1387 void stabs32_output(int type, void *param)
1389 struct symlininfo *s;
1390 struct linelist *el;
1391 if (type == TY_STABSSYMLIN) {
1392 if (stabs_immcall) {
1393 s = (struct symlininfo *)param;
1394 if (strcmp(s->name, ".text"))
1395 return; /* we are only interested in the text stuff */
1396 numlinestabs++;
1397 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1398 el->info.offset = s->offset;
1399 el->info.section = s->section;
1400 el->info.name = s->name;
1401 el->line = currentline;
1402 el->filename = stabs_filename;
1403 el->next = 0;
1404 if (stabslines) {
1405 stabslines->last->next = el;
1406 stabslines->last = el;
1407 } else {
1408 stabslines = el;
1409 stabslines->last = el;
1413 stabs_immcall = 0;
1416 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1417 do {\
1418 WRITELONG(p,n_strx); \
1419 WRITECHAR(p,n_type); \
1420 WRITECHAR(p,n_other); \
1421 WRITESHORT(p,n_desc); \
1422 WRITELONG(p,n_value); \
1423 } while (0)
1425 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1427 void stabs32_generate(void)
1429 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1430 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1431 char **allfiles;
1432 int *fileidx;
1434 struct linelist *ptr;
1436 ptr = stabslines;
1438 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1439 for (i = 0; i < numlinestabs; i++)
1440 allfiles[i] = 0;
1441 numfiles = 0;
1442 while (ptr) {
1443 if (numfiles == 0) {
1444 allfiles[0] = ptr->filename;
1445 numfiles++;
1446 } else {
1447 for (i = 0; i < numfiles; i++) {
1448 if (!strcmp(allfiles[i], ptr->filename))
1449 break;
1451 if (i >= numfiles) {
1452 allfiles[i] = ptr->filename;
1453 numfiles++;
1456 ptr = ptr->next;
1458 strsize = 1;
1459 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1460 for (i = 0; i < numfiles; i++) {
1461 fileidx[i] = strsize;
1462 strsize += strlen(allfiles[i]) + 1;
1464 mainfileindex = 0;
1465 for (i = 0; i < numfiles; i++) {
1466 if (!strcmp(allfiles[i], elf_module)) {
1467 mainfileindex = i;
1468 break;
1472 /* worst case size of the stab buffer would be:
1473 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1475 sbuf =
1476 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1477 sizeof(struct stabentry));
1479 ssbuf = (uint8_t *)nasm_malloc(strsize);
1481 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1482 rptr = rbuf;
1484 for (i = 0; i < numfiles; i++) {
1485 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1487 ssbuf[0] = 0;
1489 stabstrlen = strsize; /* set global variable for length of stab strings */
1491 sptr = sbuf;
1492 /* this is the first stab, its strx points to the filename of the
1493 the source-file, the n_desc field should be set to the number
1494 of remaining stabs
1496 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1498 ptr = stabslines;
1499 numstabs = 0;
1501 if (ptr) {
1502 /* this is the stab for the main source file */
1503 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1505 /* relocation table entry */
1507 /* Since the above WRITE_STAB calls have already */
1508 /* created two entries, the index in the info.section */
1509 /* member must be adjusted by adding 3 */
1511 WRITELONG(rptr, (sptr - sbuf) - 4);
1512 WRITELONG(rptr, ((ptr->info.section + 3) << 8) | R_386_32);
1514 numstabs++;
1515 currfile = mainfileindex;
1518 while (ptr) {
1519 if (strcmp(allfiles[currfile], ptr->filename)) {
1520 /* oops file has changed... */
1521 for (i = 0; i < numfiles; i++)
1522 if (!strcmp(allfiles[i], ptr->filename))
1523 break;
1524 currfile = i;
1525 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1526 ptr->info.offset);
1527 numstabs++;
1529 /* relocation table entry */
1530 WRITELONG(rptr, (sptr - sbuf) - 4);
1531 WRITELONG(rptr, ((ptr->info.section + 3) << 8) | R_386_32);
1534 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1535 numstabs++;
1537 /* relocation table entry */
1539 WRITELONG(rptr, (sptr - sbuf) - 4);
1540 WRITELONG(rptr, ((ptr->info.section + 3) << 8) | R_386_32);
1542 ptr = ptr->next;
1546 ((struct stabentry *)sbuf)->n_desc = numstabs;
1548 nasm_free(allfiles);
1549 nasm_free(fileidx);
1551 stablen = (sptr - sbuf);
1552 stabrellen = (rptr - rbuf);
1553 stabrelbuf = rbuf;
1554 stabbuf = sbuf;
1555 stabstrbuf = ssbuf;
1558 void stabs32_cleanup()
1560 struct linelist *ptr, *del;
1561 if (!stabslines)
1562 return;
1563 ptr = stabslines;
1564 while (ptr) {
1565 del = ptr;
1566 ptr = ptr->next;
1567 nasm_free(del);
1569 if (stabbuf)
1570 nasm_free(stabbuf);
1571 if (stabrelbuf)
1572 nasm_free(stabrelbuf);
1573 if (stabstrbuf)
1574 nasm_free(stabstrbuf);
1577 #endif /* OF_ELF */