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

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