12 #include <gpxe/tables.h>
16 #define eprintf(...) fprintf ( stderr, __VA_ARGS__ )
18 #define dprintf(...) do { \
20 fprintf ( stderr, __VA_ARGS__ ); \
26 * Fix up ICC alignments
29 * @ret rc Return status code
31 * See comments in tables.h for an explanation of why this monstrosity
34 static int ICCFIX ( void *elf ) {
36 ELF_SHDR *shdr = ( elf + ehdr->e_shoff );
37 size_t shentsize = ehdr->e_shentsize;
38 unsigned int shnum = ehdr->e_shnum;
39 ELF_SHDR *strtab = ( ( ( void * ) shdr ) +
40 ( ehdr->e_shstrndx * shentsize ) );
41 char *strings = ( elf + strtab->sh_offset );
43 for ( ; shnum-- ; shdr = ( ( ( void * ) shdr ) + shentsize ) ) {
44 char *name = ( strings + shdr->sh_name );
45 unsigned long align = shdr->sh_addralign;
46 unsigned long new_align;
48 if ( ( strncmp ( name, ".tbl.", 5 ) == 0 ) &&
49 ( align >= ICC_ALIGN_HACK_FACTOR ) ) {
50 new_align = ( align / ICC_ALIGN_HACK_FACTOR );
51 shdr->sh_addralign = new_align;
52 dprintf ( "Section \"%s\": alignment %d->%d\n",
53 name, align, new_align );
59 #else /* SELF_INCLUDED */
63 /* Include iccfix32() function */
64 #define ELF_EHDR Elf32_Ehdr
65 #define ELF_SHDR Elf32_Shdr
66 #define ICCFIX iccfix32
72 /* Include iccfix64() function */
73 #define ELF_EHDR Elf64_Ehdr
74 #define ELF_SHDR Elf64_Shdr
75 #define ICCFIX iccfix64
81 static int iccfix ( const char *filename ) {
85 unsigned char *eident;
88 /* Open and mmap file */
89 fd = open ( filename, O_RDWR );
91 eprintf ( "Could not open %s: %s\n",
92 filename, strerror ( errno ) );
96 if ( fstat ( fd, &stat ) < 0 ) {
97 eprintf ( "Could not determine size of %s: %s\n",
98 filename, strerror ( errno ) );
102 elf = mmap ( NULL, stat.st_size, ( PROT_READ | PROT_WRITE ),
104 if ( elf == MAP_FAILED ) {
105 eprintf ( "Could not map %s: %s\n",
106 filename, strerror ( errno ) );
113 switch ( eident[EI_CLASS] ) {
115 rc = iccfix32 ( elf );
118 rc = iccfix64 ( elf );
121 eprintf ( "Unknown ELF class %d in %s\n",
122 eident[EI_CLASS], filename );
127 munmap ( elf, stat.st_size );
135 int main ( int argc, char **argv ) {
139 /* Parse command line */
141 eprintf ( "Syntax: %s <object_file>...\n", argv[0] );
145 /* Process each object in turn */
146 for ( i = 1 ; i < argc ; i++ ) {
147 if ( ( rc = iccfix ( argv[i] ) ) != 0 ) {
148 eprintf ( "Could not fix up %s\n", argv[i] );
156 #endif /* SELF_INCLUDED */