Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 3 | * All rights reserved. |
Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 4 | * |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 14 | * |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 27 | */ |
| 28 | |
Elliott Hughes | cbc80ba | 2018-02-13 14:26:29 -0800 | [diff] [blame] | 29 | #pragma once |
Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 30 | |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 31 | #include <string.h> |
| 32 | |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 33 | #include "linker.h" |
| 34 | #include "linker_sleb128.h" |
| 35 | |
Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 36 | const size_t RELOCATION_GROUPED_BY_INFO_FLAG = 1; |
| 37 | const size_t RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG = 2; |
| 38 | const size_t RELOCATION_GROUPED_BY_ADDEND_FLAG = 4; |
| 39 | const size_t RELOCATION_GROUP_HAS_ADDEND_FLAG = 8; |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 40 | |
Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 41 | #if defined(USE_RELA) |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 42 | typedef ElfW(Rela) rel_t; |
Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 43 | #else |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 44 | typedef ElfW(Rel) rel_t; |
Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 45 | #endif |
Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 46 | |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 47 | template <typename F> |
| 48 | inline bool for_all_packed_relocs(sleb128_decoder decoder, F&& callback) { |
| 49 | const size_t num_relocs = decoder.pop_front(); |
Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 50 | |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 51 | rel_t reloc = { |
| 52 | .r_offset = decoder.pop_front(), |
| 53 | }; |
Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 54 | |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 55 | for (size_t idx = 0; idx < num_relocs; ) { |
| 56 | const size_t group_size = decoder.pop_front(); |
| 57 | const size_t group_flags = decoder.pop_front(); |
Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 58 | |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 59 | size_t group_r_offset_delta = 0; |
| 60 | |
| 61 | if (group_flags & RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG) { |
| 62 | group_r_offset_delta = decoder.pop_front(); |
| 63 | } |
| 64 | if (group_flags & RELOCATION_GROUPED_BY_INFO_FLAG) { |
| 65 | reloc.r_info = decoder.pop_front(); |
| 66 | } |
| 67 | |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 68 | #if defined(USE_RELA) |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 69 | const size_t group_flags_reloc = group_flags & (RELOCATION_GROUP_HAS_ADDEND_FLAG | |
| 70 | RELOCATION_GROUPED_BY_ADDEND_FLAG); |
| 71 | if (group_flags_reloc == RELOCATION_GROUP_HAS_ADDEND_FLAG) { |
| 72 | // Each relocation has an addend. This is the default situation with lld's current encoder. |
| 73 | } else if (group_flags_reloc == (RELOCATION_GROUP_HAS_ADDEND_FLAG | |
| 74 | RELOCATION_GROUPED_BY_ADDEND_FLAG)) { |
| 75 | reloc.r_addend += decoder.pop_front(); |
| 76 | } else { |
| 77 | reloc.r_addend = 0; |
| 78 | } |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 79 | #else |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 80 | if (__predict_false(group_flags & RELOCATION_GROUP_HAS_ADDEND_FLAG)) { |
| 81 | // This platform does not support rela, and yet we have it encoded in android_rel section. |
| 82 | async_safe_fatal("unexpected r_addend in android.rel section"); |
| 83 | } |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 84 | #endif |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 85 | |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 86 | for (size_t i = 0; i < group_size; ++i) { |
| 87 | if (group_flags & RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG) { |
| 88 | reloc.r_offset += group_r_offset_delta; |
| 89 | } else { |
| 90 | reloc.r_offset += decoder.pop_front(); |
| 91 | } |
| 92 | if ((group_flags & RELOCATION_GROUPED_BY_INFO_FLAG) == 0) { |
| 93 | reloc.r_info = decoder.pop_front(); |
| 94 | } |
| 95 | #if defined(USE_RELA) |
| 96 | if (group_flags_reloc == RELOCATION_GROUP_HAS_ADDEND_FLAG) { |
| 97 | reloc.r_addend += decoder.pop_front(); |
| 98 | } |
| 99 | #endif |
| 100 | if (!callback(reloc)) { |
| 101 | return false; |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 105 | idx += group_size; |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 108 | return true; |
| 109 | } |