]>
git.saurik.com Git - apple/boot.git/blob - i386/nasm/outelf.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
24 /* outelf.c output routines for the Netwide Assembler to produce
25 * ELF32 (i386 of course) object file format
27 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
28 * Julian Hall. All rights reserved. The software is
29 * redistributable under the licence given in the file "Licence"
30 * distributed in the NASM archive.
47 #define R_386_32 1 /* ordinary absolute relocation */
48 #define R_386_PC32 2 /* PC-relative relocation */
49 #define R_386_GOT32 3 /* an offset into GOT */
50 #define R_386_PLT32 4 /* a PC-relative offset into PLT */
51 #define R_386_GOTOFF 9 /* an offset from GOT base */
52 #define R_386_GOTPC 10 /* a PC-relative offset _to_ GOT */
56 long address
; /* relative to _start_ of section */
57 long symbol
; /* ELF symbol info thingy */
58 int type
; /* type of relocation */
62 long strpos
; /* string table position of name */
63 long section
; /* section ID of the symbol */
64 int type
; /* symbol type */
65 long value
; /* address, or COMMON variable align */
66 long size
; /* size of symbol */
67 long globnum
; /* symbol table offset if global */
68 struct Symbol
*next
; /* list of globals in each section */
69 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
70 char *name
; /* used temporarily if in above list */
73 #define SHT_PROGBITS 1
78 #define SHF_EXECINSTR 4
82 unsigned long len
, size
, nrelocs
;
84 int type
; /* SHT_PROGBITS or SHT_NOBITS */
85 int align
; /* alignment: power of two */
86 unsigned long flags
; /* section flags */
90 struct Reloc
*head
, **tail
;
91 struct Symbol
*gsyms
; /* global symbols in section */
95 static struct Section
**sects
;
96 static int nsects
, sectlen
;
98 #define SHSTR_DELTA 256
99 static char *shstrtab
;
100 static int shstrtablen
, shstrtabsize
;
102 static struct SAA
*syms
;
103 static unsigned long nlocals
, nglobs
;
107 static struct RAA
*bsym
;
109 static struct SAA
*strs
;
110 static unsigned long strslen
;
114 static evalfunc evaluate
;
116 static struct Symbol
*fwds
;
118 static char elf_module
[FILENAME_MAX
];
120 extern struct ofmt of_elf
;
122 #define SHN_ABS 0xFFF1
123 #define SHN_COMMON 0xFFF2
126 #define SYM_SECTION 0x04
127 #define SYM_GLOBAL 0x10
128 #define SYM_DATA 0x01
129 #define SYM_FUNCTION 0x02
131 #define GLOBAL_TEMP_BASE 6 /* bigger than any constant sym id */
133 #define SEG_ALIGN 16 /* alignment of sections in file */
134 #define SEG_ALIGN_1 (SEG_ALIGN-1)
136 static const char align_str
[SEG_ALIGN
] = ""; /* ANSI will pad this with 0s */
138 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
139 static struct ELF_SECTDATA
{
143 } elf_sects
[ELF_MAX_SECTIONS
];
144 static int elf_nsect
;
145 static long elf_foffs
;
147 static void elf_write(void);
148 static void elf_sect_write(struct Section
*, unsigned char *, unsigned long);
149 static void elf_section_header (int, int, int, void *, int, long,
151 static void elf_write_sections (void);
152 static struct SAA
*elf_build_symtab (long *, long *);
153 static struct SAA
*elf_build_reltab (long *, struct Reloc
*);
154 static void add_sectname (char *, char *);
157 * Special section numbers which are used to define ELF special
158 * symbols, which can be used with WRT to provide PIC relocation
161 static long elf_gotpc_sect
, elf_gotoff_sect
;
162 static long elf_got_sect
, elf_plt_sect
;
163 static long elf_sym_sect
;
165 static void elf_init(FILE *fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
) {
169 (void) ldef
; /* placate optimisers */
171 nsects
= sectlen
= 0;
172 syms
= saa_init((long)sizeof(struct Symbol
));
173 nlocals
= nglobs
= 0;
176 saa_wbytes (strs
, "\0", 1L);
177 saa_wbytes (strs
, elf_module
, (long)(strlen(elf_module
)+1));
178 strslen
= 2+strlen(elf_module
);
180 shstrtablen
= shstrtabsize
= 0;;
181 add_sectname ("", "");
185 elf_gotpc_sect
= seg_alloc();
186 ldef("..gotpc", elf_gotpc_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
187 elf_gotoff_sect
= seg_alloc();
188 ldef("..gotoff", elf_gotoff_sect
+1, 0L, NULL
, FALSE
, FALSE
,&of_elf
,error
);
189 elf_got_sect
= seg_alloc();
190 ldef("..got", elf_got_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
191 elf_plt_sect
= seg_alloc();
192 ldef("..plt", elf_plt_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
193 elf_sym_sect
= seg_alloc();
194 ldef("..sym", elf_sym_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
196 def_seg
= seg_alloc();
199 static void elf_cleanup(void) {
205 for (i
=0; i
<nsects
; i
++) {
206 if (sects
[i
]->type
!= SHT_NOBITS
)
207 saa_free (sects
[i
]->data
);
209 saa_free (sects
[i
]->rel
);
210 while (sects
[i
]->head
) {
212 sects
[i
]->head
= sects
[i
]->head
->next
;
222 static void add_sectname (char *firsthalf
, char *secondhalf
) {
223 int len
= strlen(firsthalf
)+strlen(secondhalf
);
224 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
225 shstrtab
= nasm_realloc (shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
226 strcpy (shstrtab
+shstrtablen
, firsthalf
);
227 strcat (shstrtab
+shstrtablen
, secondhalf
);
228 shstrtablen
+= len
+1;
231 static int elf_make_section (char *name
, int type
, int flags
, int align
) {
234 s
= nasm_malloc (sizeof(*s
));
236 if (type
!= SHT_NOBITS
)
237 s
->data
= saa_init (1L);
240 s
->len
= s
->size
= 0;
242 if (!strcmp(name
, ".text"))
245 s
->index
= seg_alloc();
246 add_sectname ("", name
);
247 s
->name
= nasm_malloc (1+strlen(name
));
248 strcpy (s
->name
, name
);
254 if (nsects
>= sectlen
)
255 sects
= nasm_realloc (sects
, (sectlen
+= SECT_DELTA
)*sizeof(*sects
));
261 static long elf_section_names (char *name
, int pass
, int *bits
) {
263 int flags_and
, flags_or
, type
, align
, i
;
266 * Default is 32 bits.
275 while (*p
&& !isspace(*p
)) p
++;
277 flags_and
= flags_or
= type
= align
= 0;
279 while (*p
&& isspace(*p
)) p
++;
282 while (*p
&& !isspace(*p
)) p
++;
284 while (*p
&& isspace(*p
)) p
++;
286 if (!nasm_strnicmp(q
, "align=", 6)) {
290 if ( (align
-1) & align
) { /* means it's not a power of two */
291 error (ERR_NONFATAL
, "section alignment %d is not"
292 " a power of two", align
);
295 } else if (!nasm_stricmp(q
, "alloc")) {
296 flags_and
|= SHF_ALLOC
;
297 flags_or
|= SHF_ALLOC
;
298 } else if (!nasm_stricmp(q
, "noalloc")) {
299 flags_and
|= SHF_ALLOC
;
300 flags_or
&= ~SHF_ALLOC
;
301 } else if (!nasm_stricmp(q
, "exec")) {
302 flags_and
|= SHF_EXECINSTR
;
303 flags_or
|= SHF_EXECINSTR
;
304 } else if (!nasm_stricmp(q
, "noexec")) {
305 flags_and
|= SHF_EXECINSTR
;
306 flags_or
&= ~SHF_EXECINSTR
;
307 } else if (!nasm_stricmp(q
, "write")) {
308 flags_and
|= SHF_WRITE
;
309 flags_or
|= SHF_WRITE
;
310 } else if (!nasm_stricmp(q
, "nowrite")) {
311 flags_and
|= SHF_WRITE
;
312 flags_or
&= ~SHF_WRITE
;
313 } else if (!nasm_stricmp(q
, "progbits")) {
315 } else if (!nasm_stricmp(q
, "nobits")) {
320 if (!strcmp(name
, ".comment") ||
321 !strcmp(name
, ".shstrtab") ||
322 !strcmp(name
, ".symtab") ||
323 !strcmp(name
, ".strtab")) {
324 error (ERR_NONFATAL
, "attempt to redefine reserved section"
329 for (i
=0; i
<nsects
; i
++)
330 if (!strcmp(name
, sects
[i
]->name
))
333 if (!strcmp(name
, ".text"))
334 i
= elf_make_section (name
, SHT_PROGBITS
,
335 SHF_ALLOC
| SHF_EXECINSTR
, 16);
336 else if (!strcmp(name
, ".data"))
337 i
= elf_make_section (name
, SHT_PROGBITS
,
338 SHF_ALLOC
| SHF_WRITE
, 4);
339 else if (!strcmp(name
, ".bss"))
340 i
= elf_make_section (name
, SHT_NOBITS
,
341 SHF_ALLOC
| SHF_WRITE
, 4);
343 i
= elf_make_section (name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
345 sects
[i
]->type
= type
;
347 sects
[i
]->align
= align
;
348 sects
[i
]->flags
&= ~flags_and
;
349 sects
[i
]->flags
|= flags_or
;
350 } else if (pass
== 1) {
351 if (type
|| align
|| flags_and
)
352 error (ERR_WARNING
, "section attributes ignored on"
353 " redeclaration of section `%s'", name
);
356 return sects
[i
]->index
;
359 static void elf_deflabel (char *name
, long segment
, long offset
,
360 int is_global
, char *special
) {
363 int special_used
= FALSE
;
365 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
367 * This is a NASM special symbol. We never allow it into
368 * the ELF symbol table, even if it's a valid one. If it
369 * _isn't_ a valid one, we should barf immediately.
371 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
372 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
373 strcmp(name
, "..sym"))
374 error (ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
378 if (is_global
== 3) {
381 * Fix up a forward-reference symbol size from the first
384 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
385 if (!strcmp((*s
)->name
, name
)) {
386 struct tokenval tokval
;
390 while (*p
&& !isspace(*p
)) p
++;
391 while (*p
&& isspace(*p
)) p
++;
394 tokval
.t_type
= TOKEN_INVALID
;
395 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
398 error (ERR_NONFATAL
, "cannot use relocatable"
399 " expression as symbol size");
401 (*s
)->size
= reloc_value(e
);
405 * Remove it from the list of unresolved sizes.
407 nasm_free ((*s
)->name
);
411 return; /* it wasn't an important one */
414 saa_wbytes (strs
, name
, (long)(1+strlen(name
)));
415 strslen
+= 1+strlen(name
);
417 sym
= saa_wstruct (syms
);
420 sym
->type
= is_global
? SYM_GLOBAL
: 0;
422 if (segment
== NO_SEG
)
423 sym
->section
= SHN_ABS
;
426 sym
->section
= SHN_UNDEF
;
427 if (nsects
== 0 && segment
== def_seg
) {
429 if (segment
!= elf_section_names (".text", 2, &tempint
))
430 error (ERR_PANIC
, "strange segment conditions in ELF driver");
431 sym
->section
= nsects
;
433 for (i
=0; i
<nsects
; i
++)
434 if (segment
== sects
[i
]->index
) {
441 if (is_global
== 2) {
444 sym
->section
= SHN_COMMON
;
446 * We have a common variable. Check the special text to see
447 * if it's a valid number and power of two; if so, store it
448 * as the alignment for the common variable.
452 sym
->value
= readnum (special
, &err
);
454 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
455 " valid number", special
);
456 else if ( (sym
->value
| (sym
->value
-1)) != 2*sym
->value
- 1)
457 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
458 " power of two", special
);
462 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
464 if (sym
->type
== SYM_GLOBAL
) {
465 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
)
466 bsym
= raa_write (bsym
, segment
, nglobs
);
469 * This is a global symbol; so we must add it to the linked
470 * list of global symbols in its section. We'll push it on
471 * the beginning of the list, because it doesn't matter
472 * much which end we put it on and it's easier like this.
474 * In addition, we check the special text for symbol
475 * type and size information.
477 sym
->next
= sects
[sym
->section
-1]->gsyms
;
478 sects
[sym
->section
-1]->gsyms
= sym
;
481 int n
= strcspn(special
, " ");
483 if (!nasm_strnicmp(special
, "function", n
))
484 sym
->type
|= SYM_FUNCTION
;
485 else if (!nasm_strnicmp(special
, "data", n
) ||
486 !nasm_strnicmp(special
, "object", n
))
487 sym
->type
|= SYM_DATA
;
489 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
492 struct tokenval tokval
;
496 while (special
[n
] && isspace(special
[n
]))
499 * We have a size expression; attempt to
503 stdscan_bufptr
= special
+n
;
504 tokval
.t_type
= TOKEN_INVALID
;
505 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
, NULL
);
509 sym
->name
= nasm_strdup(name
);
512 error (ERR_NONFATAL
, "cannot use relocatable"
513 " expression as symbol size");
515 sym
->size
= reloc_value(e
);
521 sym
->globnum
= nglobs
;
526 if (special
&& !special_used
)
527 error(ERR_NONFATAL
, "no special symbol features supported here");
530 static void elf_add_reloc (struct Section
*sect
, long segment
,
534 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
535 sect
->tail
= &r
->next
;
538 r
->address
= sect
->len
;
539 if (segment
== NO_SEG
)
544 for (i
=0; i
<nsects
; i
++)
545 if (segment
== sects
[i
]->index
)
548 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
556 * This routine deals with ..got and ..sym relocations: the more
557 * complicated kinds. In shared-library writing, some relocations
558 * with respect to global symbols must refer to the precise symbol
559 * rather than referring to an offset from the base of the section
560 * _containing_ the symbol. Such relocations call to this routine,
561 * which searches the symbol list for the symbol in question.
563 * R_386_GOT32 references require the _exact_ symbol address to be
564 * used; R_386_32 references can be at an offset from the symbol.
565 * The boolean argument `exact' tells us this.
567 * Return value is the adjusted value of `addr', having become an
568 * offset from the symbol rather than the section. Should always be
569 * zero when returning from an exact call.
571 * Limitation: if you define two symbols at the same place,
572 * confusion will occur.
574 * Inefficiency: we search, currently, using a linked list which
575 * isn't even necessarily sorted.
577 static long elf_add_gsym_reloc (struct Section
*sect
,
578 long segment
, long offset
,
579 int type
, int exact
) {
582 struct Symbol
*sym
, *sm
;
586 * First look up the segment/offset pair and find a global
587 * symbol corresponding to it. If it's not one of our segments,
588 * then it must be an external symbol, in which case we're fine
589 * doing a normal elf_add_reloc after first sanity-checking
590 * that the offset from the symbol is zero.
593 for (i
=0; i
<nsects
; i
++)
594 if (segment
== sects
[i
]->index
) {
599 if (exact
&& offset
!= 0)
600 error (ERR_NONFATAL
, "unable to find a suitable global symbol"
601 " for this reference");
603 elf_add_reloc (sect
, segment
, type
);
609 * Find a symbol pointing _exactly_ at this one.
611 for (sym
= s
->gsyms
; sym
; sym
= sym
->next
)
612 if (sym
->value
== offset
)
616 * Find the nearest symbol below this one.
619 for (sm
= s
->gsyms
; sm
; sm
= sm
->next
)
620 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
624 error (ERR_NONFATAL
, "unable to find a suitable global symbol"
625 " for this reference");
629 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
630 sect
->tail
= &r
->next
;
633 r
->address
= sect
->len
;
634 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
639 return offset
- sym
->value
;
642 static void elf_out (long segto
, void *data
, unsigned long type
,
643 long segment
, long wrt
) {
645 long realbytes
= type
& OUT_SIZMASK
;
647 unsigned char mydata
[4], *p
;
653 * handle absolute-assembly (structure definitions)
655 if (segto
== NO_SEG
) {
656 if (type
!= OUT_RESERVE
)
657 error (ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
663 for (i
=0; i
<nsects
; i
++)
664 if (segto
== sects
[i
]->index
) {
669 int tempint
; /* ignored */
670 if (segto
!= elf_section_names (".text", 2, &tempint
))
671 error (ERR_PANIC
, "strange segment conditions in ELF driver");
676 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
677 error(ERR_WARNING
, "attempt to initialise memory in"
678 " BSS section `%s': ignored", s
->name
);
679 if (type
== OUT_REL2ADR
)
681 else if (type
== OUT_REL4ADR
)
687 if (type
== OUT_RESERVE
) {
688 if (s
->type
== SHT_PROGBITS
) {
689 error(ERR_WARNING
, "uninitialised space declared in"
690 " non-BSS section `%s': zeroing", s
->name
);
691 elf_sect_write (s
, NULL
, realbytes
);
694 } else if (type
== OUT_RAWDATA
) {
695 if (segment
!= NO_SEG
)
696 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
697 elf_sect_write (s
, data
, realbytes
);
698 } else if (type
== OUT_ADDRESS
) {
699 addr
= *(long *)data
;
700 if (segment
!= NO_SEG
) {
702 error(ERR_NONFATAL
, "ELF format does not support"
703 " segment base references");
706 elf_add_reloc (s
, segment
, R_386_32
);
707 } else if (wrt
== elf_gotpc_sect
+1) {
709 * The user will supply GOT relative to $$. ELF
710 * will let us have GOT relative to $. So we
711 * need to fix up the data item by $-$$.
714 elf_add_reloc (s
, segment
, R_386_GOTPC
);
715 } else if (wrt
== elf_gotoff_sect
+1) {
716 elf_add_reloc (s
, segment
, R_386_GOTOFF
);
717 } else if (wrt
== elf_got_sect
+1) {
718 addr
= elf_add_gsym_reloc (s
, segment
, addr
,
720 } else if (wrt
== elf_sym_sect
+1) {
721 addr
= elf_add_gsym_reloc (s
, segment
, addr
,
723 } else if (wrt
== elf_plt_sect
+1) {
724 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
725 "relative PLT references");
727 error (ERR_NONFATAL
, "ELF format does not support this"
729 wrt
= NO_SEG
; /* we can at least _try_ to continue */
734 if (realbytes
!= 4 && segment
!= NO_SEG
)
735 error (ERR_NONFATAL
, "ELF format does not support non-32-bit"
738 elf_sect_write (s
, mydata
, realbytes
);
739 } else if (type
== OUT_REL2ADR
) {
740 error (ERR_NONFATAL
, "ELF format does not support 16-bit"
742 } else if (type
== OUT_REL4ADR
) {
743 if (segment
== segto
)
744 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
745 if (segment
!= NO_SEG
&& segment
% 2) {
746 error(ERR_NONFATAL
, "ELF format does not support"
747 " segment base references");
750 elf_add_reloc (s
, segment
, R_386_PC32
);
751 } else if (wrt
== elf_plt_sect
+1) {
752 elf_add_reloc (s
, segment
, R_386_PLT32
);
753 } else if (wrt
== elf_gotpc_sect
+1 ||
754 wrt
== elf_gotoff_sect
+1 ||
755 wrt
== elf_got_sect
+1) {
756 error(ERR_NONFATAL
, "ELF format cannot produce PC-"
757 "relative GOT references");
759 error (ERR_NONFATAL
, "ELF format does not support this"
761 wrt
= NO_SEG
; /* we can at least _try_ to continue */
765 WRITELONG (p
, *(long*)data
- realbytes
);
766 elf_sect_write (s
, mydata
, 4L);
770 static void elf_write(void) {
771 int nsections
, align
;
778 long symtablen
, symtablocal
;
781 * Work out how many sections we will have. We have SHN_UNDEF,
782 * then the flexible user sections, then the four fixed
783 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
784 * then optionally relocation sections for the user sections.
786 nsections
= 5; /* SHN_UNDEF and the fixed ones */
787 add_sectname ("", ".comment");
788 add_sectname ("", ".shstrtab");
789 add_sectname ("", ".symtab");
790 add_sectname ("", ".strtab");
791 for (i
=0; i
<nsects
; i
++) {
792 nsections
++; /* for the section itself */
793 if (sects
[i
]->head
) {
794 nsections
++; /* for its relocations */
795 add_sectname (".rel", sects
[i
]->name
);
803 commlen
= 2+sprintf(comment
+1, "The Netwide Assembler %s", NASM_VER
);
806 * Output the ELF header.
808 fwrite ("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp
);
809 fwriteshort (1, elffp
); /* ET_REL relocatable file */
810 fwriteshort (3, elffp
); /* EM_386 processor ID */
811 fwritelong (1L, elffp
); /* EV_CURRENT file format version */
812 fwritelong (0L, elffp
); /* no entry point */
813 fwritelong (0L, elffp
); /* no program header table */
814 fwritelong (0x40L
, elffp
); /* section headers straight after
815 * ELF header plus alignment */
816 fwritelong (0L, elffp
); /* 386 defines no special flags */
817 fwriteshort (0x34, elffp
); /* size of ELF header */
818 fwriteshort (0, elffp
); /* no program header table, again */
819 fwriteshort (0, elffp
); /* still no program header table */
820 fwriteshort (0x28, elffp
); /* size of section header */
821 fwriteshort (nsections
, elffp
); /* number of sections */
822 fwriteshort (nsects
+2, elffp
); /* string table section index for
823 * section header table */
824 fwritelong (0L, elffp
); /* align to 0x40 bytes */
825 fwritelong (0L, elffp
);
826 fwritelong (0L, elffp
);
829 * Build the symbol table and relocation tables.
831 symtab
= elf_build_symtab (&symtablen
, &symtablocal
);
832 for (i
=0; i
<nsects
; i
++)
834 sects
[i
]->rel
= elf_build_reltab (§s
[i
]->rellen
,
838 * Now output the section header table.
841 elf_foffs
= 0x40 + 0x28 * nsections
;
842 align
= ((elf_foffs
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
846 elf_section_header (0, 0, 0, NULL
, FALSE
, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
848 for (i
=0; i
<nsects
; i
++) {
849 elf_section_header (p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
850 (sects
[i
]->type
== SHT_PROGBITS
?
851 sects
[i
]->data
: NULL
), TRUE
,
852 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
855 elf_section_header (p
- shstrtab
, 1, 0, comment
, FALSE
,
856 (long)commlen
, 0, 0, 1, 0);/* .comment */
858 elf_section_header (p
- shstrtab
, 3, 0, shstrtab
, FALSE
,
859 (long)shstrtablen
, 0, 0, 1, 0);/* .shstrtab */
861 elf_section_header (p
- shstrtab
, 2, 0, symtab
, TRUE
,
862 symtablen
, nsects
+4, symtablocal
, 4, 16);/* .symtab */
864 elf_section_header (p
- shstrtab
, 3, 0, strs
, TRUE
,
865 strslen
, 0, 0, 1, 0); /* .strtab */
866 for (i
=0; i
<nsects
; i
++) if (sects
[i
]->head
) {
868 elf_section_header (p
- shstrtab
, 9, 0, sects
[i
]->rel
, TRUE
,
869 sects
[i
]->rellen
, nsects
+3, i
+1, 4, 8);
872 fwrite (align_str
, align
, 1, elffp
);
875 * Now output the sections.
877 elf_write_sections();
882 static struct SAA
*elf_build_symtab (long *len
, long *local
) {
883 struct SAA
*s
= saa_init(1L);
885 unsigned char entry
[16], *p
;
891 * First, an all-zeros entry, required by the ELF spec.
893 saa_wbytes (s
, NULL
, 16L); /* null symbol table entry */
898 * Next, an entry for the file name.
901 WRITELONG (p
, 1); /* we know it's 1st thing in strtab */
902 WRITELONG (p
, 0); /* no value */
903 WRITELONG (p
, 0); /* no size either */
904 WRITESHORT (p
, 4); /* type FILE */
905 WRITESHORT (p
, SHN_ABS
);
906 saa_wbytes (s
, entry
, 16L);
911 * Now some standard symbols defining the segments, for relocation
914 for (i
= 1; i
<= nsects
+1; i
++) {
916 WRITELONG (p
, 0); /* no symbol name */
917 WRITELONG (p
, 0); /* offset zero */
918 WRITELONG (p
, 0); /* size zero */
919 WRITESHORT (p
, 3); /* local section-type thing */
920 WRITESHORT (p
, (i
==1 ? SHN_ABS
: i
-1)); /* the section id */
921 saa_wbytes (s
, entry
, 16L);
927 * Now the other local symbols.
930 while ( (sym
= saa_rstruct (syms
)) ) {
931 if (sym
->type
& SYM_GLOBAL
)
934 WRITELONG (p
, sym
->strpos
);
935 WRITELONG (p
, sym
->value
);
936 WRITELONG (p
, sym
->size
);
937 WRITESHORT (p
, sym
->type
); /* local non-typed thing */
938 WRITESHORT (p
, sym
->section
);
939 saa_wbytes (s
, entry
, 16L);
945 * Now the global symbols.
948 while ( (sym
= saa_rstruct (syms
)) ) {
949 if (!(sym
->type
& SYM_GLOBAL
))
952 WRITELONG (p
, sym
->strpos
);
953 WRITELONG (p
, sym
->value
);
954 WRITELONG (p
, sym
->size
);
955 WRITESHORT (p
, sym
->type
); /* global non-typed thing */
956 WRITESHORT (p
, sym
->section
);
957 saa_wbytes (s
, entry
, 16L);
964 static struct SAA
*elf_build_reltab (long *len
, struct Reloc
*r
) {
966 unsigned char *p
, entry
[8];
975 long sym
= r
->symbol
;
977 if (sym
>= GLOBAL_TEMP_BASE
)
978 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+3) + nlocals
;
981 WRITELONG (p
, r
->address
);
982 WRITELONG (p
, (sym
<< 8) + r
->type
);
983 saa_wbytes (s
, entry
, 8L);
992 static void elf_section_header (int name
, int type
, int flags
,
993 void *data
, int is_saa
, long datalen
,
994 int link
, int info
, int align
, int eltsize
) {
995 elf_sects
[elf_nsect
].data
= data
;
996 elf_sects
[elf_nsect
].len
= datalen
;
997 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1000 fwritelong ((long)name
, elffp
);
1001 fwritelong ((long)type
, elffp
);
1002 fwritelong ((long)flags
, elffp
);
1003 fwritelong (0L, elffp
); /* no address, ever, in object files */
1004 fwritelong (type
== 0 ? 0L : elf_foffs
, elffp
);
1005 fwritelong (datalen
, elffp
);
1007 elf_foffs
+= (datalen
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1008 fwritelong ((long)link
, elffp
);
1009 fwritelong ((long)info
, elffp
);
1010 fwritelong ((long)align
, elffp
);
1011 fwritelong ((long)eltsize
, elffp
);
1014 static void elf_write_sections (void) {
1016 for (i
= 0; i
< elf_nsect
; i
++)
1017 if (elf_sects
[i
].data
) {
1018 long len
= elf_sects
[i
].len
;
1019 long reallen
= (len
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1020 long align
= reallen
- len
;
1021 if (elf_sects
[i
].is_saa
)
1022 saa_fpwrite (elf_sects
[i
].data
, elffp
);
1024 fwrite (elf_sects
[i
].data
, len
, 1, elffp
);
1025 fwrite (align_str
, align
, 1, elffp
);
1029 static void elf_sect_write (struct Section
*sect
,
1030 unsigned char *data
, unsigned long len
) {
1031 saa_wbytes (sect
->data
, data
, len
);
1035 static long elf_segbase (long segment
) {
1039 static int elf_directive (char *directive
, char *value
, int pass
) {
1043 static void elf_filename (char *inname
, char *outname
, efunc error
) {
1044 strcpy(elf_module
, inname
);
1045 standard_extension (inname
, outname
, ".o", error
);
1048 static char *elf_stdmac
[] = {
1049 "%define __SECT__ [section .text]",
1053 struct ofmt of_elf
= {
1054 "ELF32 (i386) object files (e.g. Linux)",