13 #include <ipxe/tables.h>
17 #define eprintf(...) fprintf ( stderr, __VA_ARGS__ )
19 #define dprintf(...) do { \
21 fprintf ( stderr, __VA_ARGS__ ); \
27 * Fix up ICC alignments
30 * @ret rc Return status code
32 * See comments in tables.h for an explanation of why this monstrosity
35 static int ICCFIX ( void *elf
) {
37 ELF_SHDR
*shdr
= ( elf
+ ehdr
->e_shoff
);
38 size_t shentsize
= ehdr
->e_shentsize
;
39 unsigned int shnum
= ehdr
->e_shnum
;
40 ELF_SHDR
*strtab
= ( ( ( void * ) shdr
) +
41 ( ehdr
->e_shstrndx
* shentsize
) );
42 char *strings
= ( elf
+ strtab
->sh_offset
);
44 for ( ; shnum
-- ; shdr
= ( ( ( void * ) shdr
) + shentsize
) ) {
45 char *name
= ( strings
+ shdr
->sh_name
);
46 unsigned long align
= shdr
->sh_addralign
;
47 unsigned long new_align
;
49 if ( ( strncmp ( name
, ".tbl.", 5 ) == 0 ) &&
50 ( align
>= ICC_ALIGN_HACK_FACTOR
) ) {
51 new_align
= ( align
/ ICC_ALIGN_HACK_FACTOR
);
52 shdr
->sh_addralign
= new_align
;
53 dprintf ( "Section \"%s\": alignment %ld->%ld\n",
54 name
, align
, new_align
);
60 #else /* SELF_INCLUDED */
64 /* Include iccfix32() function */
65 #define ELF_EHDR Elf32_Ehdr
66 #define ELF_SHDR Elf32_Shdr
67 #define ICCFIX iccfix32
73 /* Include iccfix64() function */
74 #define ELF_EHDR Elf64_Ehdr
75 #define ELF_SHDR Elf64_Shdr
76 #define ICCFIX iccfix64
82 static int iccfix ( const char *filename
) {
86 unsigned char *eident
;
89 /* Open and mmap file */
90 fd
= open ( filename
, O_RDWR
);
92 eprintf ( "Could not open %s: %s\n",
93 filename
, strerror ( errno
) );
97 if ( fstat ( fd
, &stat
) < 0 ) {
98 eprintf ( "Could not determine size of %s: %s\n",
99 filename
, strerror ( errno
) );
103 elf
= mmap ( NULL
, stat
.st_size
, ( PROT_READ
| PROT_WRITE
),
105 if ( elf
== MAP_FAILED
) {
106 eprintf ( "Could not map %s: %s\n",
107 filename
, strerror ( errno
) );
114 switch ( eident
[EI_CLASS
] ) {
116 rc
= iccfix32 ( elf
);
119 rc
= iccfix64 ( elf
);
122 eprintf ( "Unknown ELF class %d in %s\n",
123 eident
[EI_CLASS
], filename
);
128 munmap ( elf
, stat
.st_size
);
136 int main ( int argc
, char **argv
) {
140 /* Parse command line */
142 eprintf ( "Syntax: %s <object_file>...\n", argv
[0] );
146 /* Process each object in turn */
147 for ( i
= 1 ; i
< argc
; i
++ ) {
148 if ( ( rc
= iccfix ( argv
[i
] ) ) != 0 ) {
149 eprintf ( "Could not fix up %s\n", argv
[i
] );
157 #endif /* SELF_INCLUDED */