011-backport-mips-support-strip.patch 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. In mips64 little-endian, r_info consists of four byte fields(contains
  2. three reloc types) and a 32-bit symbol index. In order to adapt
  3. GELF_R_SYM and GELF_R_TYPE, need convert raw data to get correct symbol
  4. index and type.
  5. libelf/elf_getdata.c: Some eu-utils use read-mmap method to map file,
  6. so we need to malloc and memcpy raw data to avoid segment fault. After
  7. modification, the correct value are saved in the malloced memory not in
  8. process address space.
  9. libelf/elf_updata.c: Because we converted the relocation info in mips
  10. order when we call elf_getdata.c, so we need to convert the modified data
  11. in original order bits before writing the data to the file.
  12. Signed-off-by: Ying Huang <[email protected]>
  13. ---
  14. libelf/elf_getdata.c | 132 ++++++++++++++++++++++++++++++++++++++++++-
  15. libelf/elf_update.c | 53 +++++++++++++++++
  16. 2 files changed, 183 insertions(+), 2 deletions(-)
  17. --- a/libelf/elf_getdata.c
  18. +++ b/libelf/elf_getdata.c
  19. @@ -135,6 +135,119 @@ __libelf_data_type (GElf_Ehdr *ehdr, int
  20. /* Convert the data in the current section. */
  21. static void
  22. +convert_data_for_mips64el (Elf_Scn *scn, int eclass,
  23. + int data, size_t size, Elf_Type type)
  24. +{
  25. + /* Do we need to convert the data and/or adjust for alignment? */
  26. + if (data == MY_ELFDATA || type == ELF_T_BYTE)
  27. + {
  28. + /* In order to adapt macro GELF_R_SYM and GELF_R_TYPE on mips64, need to convert
  29. + relocation info(raw data). Some eu-utils use read-mmap method to map file, so
  30. + we need to malloc and memcpy raw data to avoid segment fault. After modification,
  31. + the correct value are saved in the malloced memory not in process address space. */
  32. + scn->data_base = malloc (size);
  33. + if (scn->data_base == NULL)
  34. + {
  35. + __libelf_seterrno (ELF_E_NOMEM);
  36. + return;
  37. + }
  38. +
  39. + /* The copy will be appropriately aligned for direct access. */
  40. + memcpy (scn->data_base, scn->rawdata_base, size);
  41. + }
  42. + else
  43. + {
  44. + xfct_t fp;
  45. +
  46. + scn->data_base = malloc (size);
  47. + if (scn->data_base == NULL)
  48. + {
  49. + __libelf_seterrno (ELF_E_NOMEM);
  50. + return;
  51. + }
  52. +
  53. + /* Make sure the source is correctly aligned for the conversion
  54. + function to directly access the data elements. */
  55. + char *rawdata_source;
  56. + /* In order to adapt macro GELF_R_SYM and GELF_R_TYPE on mips64, need to convert
  57. + relocation info(raw data). Some eu-utils use read-mmap method to map file, so
  58. + we need to malloc and memcpy raw data to avoid segment fault. After modification,
  59. + the correct value are saved in the malloced memory not in process address space. */
  60. + rawdata_source = malloc (size);
  61. + if (rawdata_source == NULL)
  62. + {
  63. + __libelf_seterrno (ELF_E_NOMEM);
  64. + return;
  65. + }
  66. +
  67. + /* The copy will be appropriately aligned for direct access. */
  68. + memcpy (rawdata_source, scn->rawdata_base, size);
  69. +
  70. + /* Get the conversion function. */
  71. + fp = __elf_xfctstom[eclass - 1][type];
  72. +
  73. + fp (scn->data_base, rawdata_source, size, 0);
  74. +
  75. + if (rawdata_source != scn->rawdata_base)
  76. + free (rawdata_source);
  77. + }
  78. +
  79. + scn->data_list.data.d.d_buf = scn->data_base;
  80. + scn->data_list.data.d.d_size = size;
  81. + scn->data_list.data.d.d_type = type;
  82. + scn->data_list.data.d.d_off = scn->rawdata.d.d_off;
  83. + scn->data_list.data.d.d_align = scn->rawdata.d.d_align;
  84. + scn->data_list.data.d.d_version = scn->rawdata.d.d_version;
  85. +
  86. + scn->data_list.data.s = scn;
  87. +
  88. + /* In mips64 little-endian, r_info consists of four byte fields(contains
  89. + three reloc types) and a 32-bit symbol index. In order to adapt
  90. + GELF_R_SYM and GELF_R_TYPE, need to convert r_info to get correct symbol
  91. + index and type. */
  92. + /* references:
  93. + https://www.linux-mips.org/pub/linux/mips/doc/ABI/elf64-2.4.pdf
  94. + Page40 && Page41 */
  95. + GElf_Shdr shdr_mem;
  96. + GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
  97. + if (shdr->sh_type == SHT_REL)
  98. + {
  99. + size_t sh_entsize = gelf_fsize (scn->elf, ELF_T_REL, 1, EV_CURRENT);
  100. + int nentries = shdr->sh_size / sh_entsize;
  101. + for (int cnt = 0; cnt < nentries; ++cnt)
  102. + {
  103. + Elf_Data_Scn *data_scn = (Elf_Data_Scn *) &scn->data_list.data.d;
  104. + Elf64_Rel *value = &((Elf64_Rel *) data_scn->d.d_buf)[cnt];
  105. + Elf64_Xword info = value->r_info;
  106. + value->r_info = (((info & 0xffffffff) << 32)
  107. + | ((info >> 56) & 0xff)
  108. + | ((info >> 40) & 0xff00)
  109. + | ((info >> 24) & 0xff0000)
  110. + | ((info >> 8) & 0xff000000));
  111. + ((Elf64_Rel *) data_scn->d.d_buf)[cnt] = *value;
  112. + }
  113. + }
  114. + else if (shdr->sh_type == SHT_RELA)
  115. + {
  116. + size_t sh_entsize = gelf_fsize (scn->elf, ELF_T_RELA, 1, EV_CURRENT);
  117. + int nentries = shdr->sh_size / sh_entsize;
  118. + for (int cnt = 0; cnt < nentries; cnt++)
  119. + {
  120. + Elf_Data_Scn *data_scn = (Elf_Data_Scn *) &scn->data_list.data.d;
  121. + Elf64_Rela *value = &((Elf64_Rela *) data_scn->d.d_buf)[cnt];
  122. + Elf64_Xword info = value->r_info;
  123. + value->r_info = (((info & 0xffffffff) << 32)
  124. + | ((info >> 56) & 0xff)
  125. + | ((info >> 40) & 0xff00)
  126. + | ((info >> 24) & 0xff0000)
  127. + | ((info >> 8) & 0xff000000));
  128. + ((Elf64_Rela *) data_scn->d.d_buf)[cnt] = *value;
  129. + }
  130. + }
  131. +}
  132. +
  133. +/* Convert the data in the current section. */
  134. +static void
  135. convert_data (Elf_Scn *scn, int eclass,
  136. int data, size_t size, Elf_Type type)
  137. {
  138. @@ -451,8 +564,23 @@ __libelf_set_data_list_rdlock (Elf_Scn *
  139. return;
  140. }
  141. - /* Convert according to the version and the type. */
  142. - convert_data (scn, elf->class,
  143. + GElf_Shdr shdr_mem;
  144. + GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
  145. + GElf_Ehdr ehdr_mem;
  146. + GElf_Ehdr *ehdr = gelf_getehdr (scn->elf, &ehdr_mem);
  147. + if (shdr != NULL && (shdr->sh_type == SHT_RELA || shdr->sh_type == SHT_REL) &&
  148. + scn->elf->class == ELFCLASS64 && ehdr != NULL &&
  149. + ehdr->e_machine == EM_MIPS && ehdr->e_ident[EI_DATA] == ELFDATA2LSB)
  150. + convert_data_for_mips64el (scn, elf->class,
  151. + (elf->class == ELFCLASS32
  152. + || (offsetof (struct Elf, state.elf32.ehdr)
  153. + == offsetof (struct Elf, state.elf64.ehdr))
  154. + ? elf->state.elf32.ehdr->e_ident[EI_DATA]
  155. + : elf->state.elf64.ehdr->e_ident[EI_DATA]),
  156. + scn->rawdata.d.d_size, scn->rawdata.d.d_type);
  157. + else
  158. + /* Convert according to the version and the type. */
  159. + convert_data (scn, elf->class,
  160. (elf->class == ELFCLASS32
  161. || (offsetof (struct Elf, state.elf32.ehdr)
  162. == offsetof (struct Elf, state.elf64.ehdr))
  163. --- a/libelf/elf_update.c
  164. +++ b/libelf/elf_update.c
  165. @@ -228,7 +228,60 @@ elf_update (Elf *elf, Elf_Cmd cmd)
  166. size = -1;
  167. }
  168. else
  169. + {
  170. + /* Because we converted the relocation info in mips order when we call elf_getdata.c,
  171. + so we need to convert the modified data in original order bits before writing the
  172. + data to the file. */
  173. + Elf_Scn *scn = NULL;
  174. + while ((scn = elf_nextscn (elf, scn)) != NULL)
  175. + {
  176. + GElf_Shdr shdr_mem;
  177. + GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
  178. + GElf_Ehdr ehdr_mem;
  179. + GElf_Ehdr *ehdr = gelf_getehdr (scn->elf, &ehdr_mem);
  180. + if (shdr != NULL && (shdr->sh_type == SHT_RELA || shdr->sh_type == SHT_REL) &&
  181. + scn->elf->class == ELFCLASS64 &&
  182. + ehdr != NULL && ehdr->e_machine == EM_MIPS && ehdr->e_ident[EI_DATA] == ELFDATA2LSB)
  183. + {
  184. + Elf_Data *d = elf_getdata (scn, NULL);
  185. + if (shdr->sh_type == SHT_REL)
  186. + {
  187. + size_t sh_entsize = gelf_fsize (scn->elf, ELF_T_REL, 1, EV_CURRENT);
  188. + int nentries = shdr->sh_size / sh_entsize;
  189. + for (int cnt = 0; cnt < nentries; ++cnt)
  190. + {
  191. + Elf_Data_Scn *data_scn = (Elf_Data_Scn *) d;
  192. + Elf64_Rel *value = &((Elf64_Rel *) data_scn->d.d_buf)[cnt];
  193. + Elf64_Xword info = value->r_info;
  194. + value->r_info = (info >> 32
  195. + | ((info << 56) & 0xff00000000000000)
  196. + | ((info << 40) & 0xff000000000000)
  197. + | ((info << 24) & 0xff0000000000)
  198. + | ((info << 8) & 0xff00000000));
  199. + ((Elf64_Rel *) data_scn->d.d_buf)[cnt] = *value;
  200. + }
  201. + }
  202. + else if (shdr->sh_type == SHT_RELA)
  203. + {
  204. + size_t sh_entsize = gelf_fsize (scn->elf, ELF_T_RELA, 1, EV_CURRENT);
  205. + int nentries = shdr->sh_size / sh_entsize;
  206. + for (int cnt = 0; cnt < nentries; cnt++)
  207. + {
  208. + Elf_Data_Scn *data_scn = (Elf_Data_Scn *) d;
  209. + Elf64_Rela *value = &((Elf64_Rela *) data_scn->d.d_buf)[cnt];
  210. + Elf64_Xword info = value->r_info;
  211. + value->r_info = (info >> 32
  212. + | ((info << 56) & 0xff00000000000000)
  213. + | ((info << 40) & 0xff000000000000)
  214. + | ((info << 24) & 0xff0000000000)
  215. + | ((info << 8) & 0xff00000000));
  216. + ((Elf64_Rela *) data_scn->d.d_buf)[cnt] = *value;
  217. + }
  218. + }
  219. + }
  220. + }
  221. size = write_file (elf, size, change_bo, shnum);
  222. + }
  223. }
  224. out: