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 #include "crazy_linker_elf_relocations.h" | 5 #include "crazy_linker_elf_relocations.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 | 8 |
9 #include "crazy_linker_debug.h" | 9 #include "crazy_linker_debug.h" |
10 #include "crazy_linker_elf_symbols.h" | 10 #include "crazy_linker_elf_symbols.h" |
11 #include "crazy_linker_elf_view.h" | 11 #include "crazy_linker_elf_view.h" |
12 #include "crazy_linker_error.h" | 12 #include "crazy_linker_error.h" |
| 13 #include "crazy_linker_system.h" |
13 #include "crazy_linker_util.h" | 14 #include "crazy_linker_util.h" |
14 #include "linker_phdr.h" | 15 #include "linker_phdr.h" |
15 | 16 |
16 #define DEBUG_RELOCATIONS 0 | 17 #define DEBUG_RELOCATIONS 0 |
17 | 18 |
18 #define RLOG(...) LOG_IF(DEBUG_RELOCATIONS, __VA_ARGS__) | 19 #define RLOG(...) LOG_IF(DEBUG_RELOCATIONS, __VA_ARGS__) |
19 #define RLOG_ERRNO(...) LOG_ERRNO_IF(DEBUG_RELOCATIONS, __VA_ARGS__) | 20 #define RLOG_ERRNO(...) LOG_ERRNO_IF(DEBUG_RELOCATIONS, __VA_ARGS__) |
20 | 21 |
21 #ifndef DF_SYMBOLIC | 22 #ifndef DF_SYMBOLIC |
22 #define DF_SYMBOLIC 2 | 23 #define DF_SYMBOLIC 2 |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 error)) | 280 error)) |
280 return false; | 281 return false; |
281 if (!ApplyRelRelocs(reinterpret_cast<ELF::Rel*>(relocations_), | 282 if (!ApplyRelRelocs(reinterpret_cast<ELF::Rel*>(relocations_), |
282 relocations_size_ / sizeof(ELF::Rel), | 283 relocations_size_ / sizeof(ELF::Rel), |
283 symbols, | 284 symbols, |
284 resolver, | 285 resolver, |
285 error)) | 286 error)) |
286 return false; | 287 return false; |
287 } | 288 } |
288 | 289 |
289 else if (relocations_type_ == DT_RELA) { | 290 if (relocations_type_ == DT_RELA) { |
290 if (!ApplyRelaRelocs(reinterpret_cast<ELF::Rela*>(plt_relocations_), | 291 if (!ApplyRelaRelocs(reinterpret_cast<ELF::Rela*>(plt_relocations_), |
291 plt_relocations_size_ / sizeof(ELF::Rela), | 292 plt_relocations_size_ / sizeof(ELF::Rela), |
292 symbols, | 293 symbols, |
293 resolver, | 294 resolver, |
294 error)) | 295 error)) |
295 return false; | 296 return false; |
296 if (!ApplyRelaRelocs(reinterpret_cast<ELF::Rela*>(relocations_), | 297 if (!ApplyRelaRelocs(reinterpret_cast<ELF::Rela*>(relocations_), |
297 relocations_size_ / sizeof(ELF::Rela), | 298 relocations_size_ / sizeof(ELF::Rela), |
298 symbols, | 299 symbols, |
299 resolver, | 300 resolver, |
(...skipping 10 matching lines...) Expand all Loading... |
310 if (phdr_table_protect_segments(phdr_, phdr_count_, load_bias_) < 0) { | 311 if (phdr_table_protect_segments(phdr_, phdr_count_, load_bias_) < 0) { |
311 error->Format("Can't reprotect loadable segments: %s", strerror(errno)); | 312 error->Format("Can't reprotect loadable segments: %s", strerror(errno)); |
312 return false; | 313 return false; |
313 } | 314 } |
314 } | 315 } |
315 | 316 |
316 LOG("%s: Done\n", __FUNCTION__); | 317 LOG("%s: Done\n", __FUNCTION__); |
317 return true; | 318 return true; |
318 } | 319 } |
319 | 320 |
| 321 #ifdef __arm__ |
| 322 // Helper class for decoding packed ARM relocation data. |
| 323 // http://en.wikipedia.org/wiki/LEB128 |
| 324 class Leb128Decoder { |
| 325 public: |
| 326 explicit Leb128Decoder(const uint8_t* encoding) |
| 327 : encoding_(encoding), cursor_(0) { } |
| 328 |
| 329 uint32_t Dequeue() { |
| 330 size_t extent = cursor_; |
| 331 while (encoding_[extent] >> 7) |
| 332 extent++; |
| 333 |
| 334 uint32_t value = 0; |
| 335 for (size_t i = extent; i > cursor_; --i) { |
| 336 value = (value << 7) | (encoding_[i] & 127); |
| 337 } |
| 338 value = (value << 7) | (encoding_[cursor_] & 127); |
| 339 |
| 340 cursor_ = extent + 1; |
| 341 return value; |
| 342 } |
| 343 |
| 344 private: |
| 345 const uint8_t* encoding_; |
| 346 size_t cursor_; |
| 347 }; |
| 348 |
| 349 bool ElfRelocations::ApplyArmPackedRelocs(const uint8_t* arm_packed_relocs, |
| 350 Error* error) { |
| 351 Leb128Decoder decoder(arm_packed_relocs); |
| 352 |
| 353 // Check for the initial APR1 header. |
| 354 if (decoder.Dequeue() != 'A' || decoder.Dequeue() != 'P' || |
| 355 decoder.Dequeue() != 'R' || decoder.Dequeue() != '1') { |
| 356 error->Format("Bad packed relocations ident, expected APR1"); |
| 357 return false; |
| 358 } |
| 359 |
| 360 // Find the count of pairs and the start address. |
| 361 size_t pairs = decoder.Dequeue(); |
| 362 const Elf32_Addr start_address = decoder.Dequeue(); |
| 363 |
| 364 // Emit initial R_ARM_RELATIVE relocation. |
| 365 Elf32_Rel relocation = {start_address, R_ARM_RELATIVE}; |
| 366 const ELF::Addr sym_addr = 0; |
| 367 const bool resolved = false; |
| 368 if (!ApplyRelReloc(&relocation, sym_addr, resolved, error)) |
| 369 return false; |
| 370 |
| 371 size_t unpacked_count = 1; |
| 372 |
| 373 // Emit relocations for each count-delta pair. |
| 374 while (pairs) { |
| 375 size_t count = decoder.Dequeue(); |
| 376 const size_t delta = decoder.Dequeue(); |
| 377 |
| 378 // Emit count R_ARM_RELATIVE relocations with delta offset. |
| 379 while (count) { |
| 380 relocation.r_offset += delta; |
| 381 if (!ApplyRelReloc(&relocation, sym_addr, resolved, error)) |
| 382 return false; |
| 383 unpacked_count++; |
| 384 count--; |
| 385 } |
| 386 pairs--; |
| 387 } |
| 388 |
| 389 RLOG("%s: unpacked_count=%d\n", __FUNCTION__, unpacked_count); |
| 390 return true; |
| 391 } |
| 392 #endif // __arm__ |
| 393 |
320 bool ElfRelocations::ApplyRelaReloc(const ELF::Rela* rela, | 394 bool ElfRelocations::ApplyRelaReloc(const ELF::Rela* rela, |
321 ELF::Addr sym_addr, | 395 ELF::Addr sym_addr, |
322 bool resolved CRAZY_UNUSED, | 396 bool resolved CRAZY_UNUSED, |
323 Error* error) { | 397 Error* error) { |
324 const ELF::Word rela_type = ELF_R_TYPE(rela->r_info); | 398 const ELF::Word rela_type = ELF_R_TYPE(rela->r_info); |
325 const ELF::Word CRAZY_UNUSED rela_symbol = ELF_R_SYM(rela->r_info); | 399 const ELF::Word CRAZY_UNUSED rela_symbol = ELF_R_SYM(rela->r_info); |
326 const ELF::Sword CRAZY_UNUSED addend = rela->r_addend; | 400 const ELF::Sword CRAZY_UNUSED addend = rela->r_addend; |
327 | 401 |
328 const ELF::Addr reloc = static_cast<ELF::Addr>(rela->r_offset + load_bias_); | 402 const ELF::Addr reloc = static_cast<ELF::Addr>(rela->r_offset + load_bias_); |
329 | 403 |
(...skipping 490 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
820 if (got_addr < src_addr || got_addr >= src_addr + size) | 894 if (got_addr < src_addr || got_addr >= src_addr + size) |
821 continue; | 895 continue; |
822 ELF::Addr* dst_ptr = reinterpret_cast<ELF::Addr*>(got_addr + dst_delta); | 896 ELF::Addr* dst_ptr = reinterpret_cast<ELF::Addr*>(got_addr + dst_delta); |
823 *dst_ptr += map_delta; | 897 *dst_ptr += map_delta; |
824 } | 898 } |
825 } | 899 } |
826 #endif | 900 #endif |
827 } | 901 } |
828 | 902 |
829 } // namespace crazy | 903 } // namespace crazy |
OLD | NEW |