OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Implementation notes: | 5 // Implementation notes: |
6 // | 6 // |
7 // We need to remove a piece from the ELF shared library. However, we also | 7 // We need to remove a piece from the ELF shared library. However, we also |
8 // want to ensure that code and data loads at the same addresses as before | 8 // want to ensure that code and data loads at the same addresses as before |
9 // packing, so that tools like breakpad can still match up addresses found | 9 // packing, so that tools like breakpad can still match up addresses found |
10 // in any crash dumps with data extracted from the pre-packed version of | 10 // in any crash dumps with data extracted from the pre-packed version of |
(...skipping 986 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
997 if (relative_relocations.empty()) { | 997 if (relative_relocations.empty()) { |
998 LOG(ERROR) << "No relative relocations found (already packed?)"; | 998 LOG(ERROR) << "No relative relocations found (already packed?)"; |
999 return false; | 999 return false; |
1000 } | 1000 } |
1001 | 1001 |
1002 // If not padding fully, apply only enough padding to preserve alignment. | 1002 // If not padding fully, apply only enough padding to preserve alignment. |
1003 // Otherwise, pad so that we do not shrink the relocations section at all. | 1003 // Otherwise, pad so that we do not shrink the relocations section at all. |
1004 if (!is_padding_relocations_) { | 1004 if (!is_padding_relocations_) { |
1005 // Calculate the size of the hole we will close up when we rewrite | 1005 // Calculate the size of the hole we will close up when we rewrite |
1006 // dynamic relocations. | 1006 // dynamic relocations. |
1007 ELF::Shdr* section_header = ELF::getshdr(relocations_section_); | |
1008 const ELF::Off hole_start = section_header->sh_offset; | |
1009 ssize_t hole_size = | 1007 ssize_t hole_size = |
1010 relative_relocations.size() * sizeof(relative_relocations[0]); | 1008 relative_relocations.size() * sizeof(relative_relocations[0]); |
1011 const ssize_t unaligned_hole_size = hole_size; | 1009 const ssize_t unaligned_hole_size = hole_size; |
1012 | 1010 |
1013 // Adjust the actual hole size to preserve alignment. We always adjust | 1011 // Adjust the actual hole size to preserve alignment. We always adjust |
1014 // by a whole number of NONE-type relocations. | 1012 // by a whole number of NONE-type relocations. |
1015 while (hole_size % kPreserveAlignment) | 1013 while (hole_size % kPreserveAlignment) |
1016 hole_size -= sizeof(relative_relocations[0]); | 1014 hole_size -= sizeof(relative_relocations[0]); |
1017 LOG(INFO) << "Compaction : " << hole_size << " bytes"; | 1015 LOG(INFO) << "Compaction : " << hole_size << " bytes"; |
1018 | 1016 |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1210 | 1208 |
1211 // If we found the same number of null relocation entries in the dynamic | 1209 // If we found the same number of null relocation entries in the dynamic |
1212 // relocations section as we hold as unpacked relative relocations, then | 1210 // relocations section as we hold as unpacked relative relocations, then |
1213 // this is a padded file. | 1211 // this is a padded file. |
1214 const bool is_padded = padding == relative_relocations.size(); | 1212 const bool is_padded = padding == relative_relocations.size(); |
1215 | 1213 |
1216 // Unless padded, report by how much we expand the file. | 1214 // Unless padded, report by how much we expand the file. |
1217 if (!is_padded) { | 1215 if (!is_padded) { |
1218 // Calculate the size of the hole we will open up when we rewrite | 1216 // Calculate the size of the hole we will open up when we rewrite |
1219 // dynamic relocations. | 1217 // dynamic relocations. |
1220 ELF::Shdr* section_header = ELF::getshdr(relocations_section_); | |
1221 const ELF::Off hole_start = section_header->sh_offset; | |
1222 ssize_t hole_size = | 1218 ssize_t hole_size = |
1223 relative_relocations.size() * sizeof(relative_relocations[0]); | 1219 relative_relocations.size() * sizeof(relative_relocations[0]); |
1224 | 1220 |
1225 // Adjust the hole size for the padding added to preserve alignment. | 1221 // Adjust the hole size for the padding added to preserve alignment. |
1226 hole_size -= padding * sizeof(other_relocations[0]); | 1222 hole_size -= padding * sizeof(other_relocations[0]); |
1227 LOG(INFO) << "Expansion : " << hole_size << " bytes"; | 1223 LOG(INFO) << "Expansion : " << hole_size << " bytes"; |
1228 } | 1224 } |
1229 | 1225 |
1230 // Rewrite the current dynamic relocations section to be the relative | 1226 // Rewrite the current dynamic relocations section to be the relative |
1231 // relocations followed by other relocations. This is the usual order in | 1227 // relocations followed by other relocations. This is the usual order in |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1280 | 1276 |
1281 // Clean up libelf, and truncate the output file to the number of bytes | 1277 // Clean up libelf, and truncate the output file to the number of bytes |
1282 // written by elf_update(). | 1278 // written by elf_update(). |
1283 elf_end(elf_); | 1279 elf_end(elf_); |
1284 elf_ = NULL; | 1280 elf_ = NULL; |
1285 const int truncate = ftruncate(fd_, file_bytes); | 1281 const int truncate = ftruncate(fd_, file_bytes); |
1286 CHECK(truncate == 0); | 1282 CHECK(truncate == 0); |
1287 } | 1283 } |
1288 | 1284 |
1289 } // namespace relocation_packer | 1285 } // namespace relocation_packer |
OLD | NEW |