Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

iccfix.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include <stdint.h>
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <errno.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <sys/mman.h>
  11. #include <elf.h>
  12. #include <ipxe/tables.h>
  13. #define DEBUG 0
  14. #define eprintf(...) fprintf ( stderr, __VA_ARGS__ )
  15. #define dprintf(...) do { \
  16. if ( DEBUG ) \
  17. fprintf ( stderr, __VA_ARGS__ ); \
  18. } while ( 0 )
  19. #ifdef SELF_INCLUDED
  20. /**
  21. * Fix up ICC alignments
  22. *
  23. * @v elf ELF header
  24. * @ret rc Return status code
  25. *
  26. * See comments in tables.h for an explanation of why this monstrosity
  27. * is necessary.
  28. */
  29. static int ICCFIX ( void *elf ) {
  30. ELF_EHDR *ehdr = elf;
  31. ELF_SHDR *shdr = ( elf + ehdr->e_shoff );
  32. size_t shentsize = ehdr->e_shentsize;
  33. unsigned int shnum = ehdr->e_shnum;
  34. ELF_SHDR *strtab = ( ( ( void * ) shdr ) +
  35. ( ehdr->e_shstrndx * shentsize ) );
  36. char *strings = ( elf + strtab->sh_offset );
  37. for ( ; shnum-- ; shdr = ( ( ( void * ) shdr ) + shentsize ) ) {
  38. char *name = ( strings + shdr->sh_name );
  39. unsigned long align = shdr->sh_addralign;
  40. unsigned long new_align;
  41. if ( ( strncmp ( name, ".tbl.", 5 ) == 0 ) &&
  42. ( align >= ICC_ALIGN_HACK_FACTOR ) ) {
  43. new_align = ( align / ICC_ALIGN_HACK_FACTOR );
  44. shdr->sh_addralign = new_align;
  45. dprintf ( "Section \"%s\": alignment %d->%d\n",
  46. name, align, new_align );
  47. }
  48. }
  49. return 0;
  50. }
  51. #else /* SELF_INCLUDED */
  52. #define SELF_INCLUDED
  53. /* Include iccfix32() function */
  54. #define ELF_EHDR Elf32_Ehdr
  55. #define ELF_SHDR Elf32_Shdr
  56. #define ICCFIX iccfix32
  57. #include "iccfix.c"
  58. #undef ELF_EHDR
  59. #undef ELF_SHDR
  60. #undef ICCFIX
  61. /* Include iccfix64() function */
  62. #define ELF_EHDR Elf64_Ehdr
  63. #define ELF_SHDR Elf64_Shdr
  64. #define ICCFIX iccfix64
  65. #include "iccfix.c"
  66. #undef ELF_EHDR
  67. #undef ELF_SHDR
  68. #undef ICCFIX
  69. static int iccfix ( const char *filename ) {
  70. int fd;
  71. struct stat stat;
  72. void *elf;
  73. unsigned char *eident;
  74. int rc;
  75. /* Open and mmap file */
  76. fd = open ( filename, O_RDWR );
  77. if ( fd < 0 ) {
  78. eprintf ( "Could not open %s: %s\n",
  79. filename, strerror ( errno ) );
  80. rc = -1;
  81. goto err_open;
  82. }
  83. if ( fstat ( fd, &stat ) < 0 ) {
  84. eprintf ( "Could not determine size of %s: %s\n",
  85. filename, strerror ( errno ) );
  86. rc = -1;
  87. goto err_fstat;
  88. }
  89. elf = mmap ( NULL, stat.st_size, ( PROT_READ | PROT_WRITE ),
  90. MAP_SHARED, fd, 0 );
  91. if ( elf == MAP_FAILED ) {
  92. eprintf ( "Could not map %s: %s\n",
  93. filename, strerror ( errno ) );
  94. rc = -1;
  95. goto err_mmap;
  96. }
  97. /* Perform fixups */
  98. eident = elf;
  99. switch ( eident[EI_CLASS] ) {
  100. case ELFCLASS32:
  101. rc = iccfix32 ( elf );
  102. break;
  103. case ELFCLASS64:
  104. rc = iccfix64 ( elf );
  105. break;
  106. default:
  107. eprintf ( "Unknown ELF class %d in %s\n",
  108. eident[EI_CLASS], filename );
  109. rc = -1;
  110. break;
  111. }
  112. munmap ( elf, stat.st_size );
  113. err_mmap:
  114. err_fstat:
  115. close ( fd );
  116. err_open:
  117. return rc;
  118. }
  119. int main ( int argc, char **argv ) {
  120. int i;
  121. int rc;
  122. /* Parse command line */
  123. if ( argc < 2 ) {
  124. eprintf ( "Syntax: %s <object_file>...\n", argv[0] );
  125. exit ( 1 );
  126. }
  127. /* Process each object in turn */
  128. for ( i = 1 ; i < argc ; i++ ) {
  129. if ( ( rc = iccfix ( argv[i] ) ) != 0 ) {
  130. eprintf ( "Could not fix up %s\n", argv[i] );
  131. exit ( 1 );
  132. }
  133. }
  134. return 0;
  135. }
  136. #endif /* SELF_INCLUDED */