1*5225e6b1SAndroid Build Coastguard Worker /*
2*5225e6b1SAndroid Build Coastguard Worker * Copyright (C) 2023 The Android Open Source Project
3*5225e6b1SAndroid Build Coastguard Worker *
4*5225e6b1SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*5225e6b1SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*5225e6b1SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*5225e6b1SAndroid Build Coastguard Worker *
8*5225e6b1SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*5225e6b1SAndroid Build Coastguard Worker *
10*5225e6b1SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*5225e6b1SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*5225e6b1SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*5225e6b1SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*5225e6b1SAndroid Build Coastguard Worker * limitations under the License.
15*5225e6b1SAndroid Build Coastguard Worker */
16*5225e6b1SAndroid Build Coastguard Worker
17*5225e6b1SAndroid Build Coastguard Worker #include <vector>
18*5225e6b1SAndroid Build Coastguard Worker
19*5225e6b1SAndroid Build Coastguard Worker #include "gtest/gtest.h"
20*5225e6b1SAndroid Build Coastguard Worker
21*5225e6b1SAndroid Build Coastguard Worker #include "elf/relocation.h"
22*5225e6b1SAndroid Build Coastguard Worker #include "libelf/elf.h"
23*5225e6b1SAndroid Build Coastguard Worker
24*5225e6b1SAndroid Build Coastguard Worker template <typename T>
25*5225e6b1SAndroid Build Coastguard Worker struct DtTags {};
26*5225e6b1SAndroid Build Coastguard Worker
27*5225e6b1SAndroid Build Coastguard Worker template <>
28*5225e6b1SAndroid Build Coastguard Worker struct DtTags<Elf64_Rela> {
29*5225e6b1SAndroid Build Coastguard Worker static constexpr uint64_t kTableTag = DT_RELA;
30*5225e6b1SAndroid Build Coastguard Worker static constexpr uint64_t kTableSizeTag = DT_RELASZ;
31*5225e6b1SAndroid Build Coastguard Worker };
32*5225e6b1SAndroid Build Coastguard Worker
33*5225e6b1SAndroid Build Coastguard Worker template <>
34*5225e6b1SAndroid Build Coastguard Worker struct DtTags<Elf64_Rel> {
35*5225e6b1SAndroid Build Coastguard Worker static constexpr uint64_t kTableTag = DT_REL;
36*5225e6b1SAndroid Build Coastguard Worker static constexpr uint64_t kTableSizeTag = DT_RELSZ;
37*5225e6b1SAndroid Build Coastguard Worker };
38*5225e6b1SAndroid Build Coastguard Worker
39*5225e6b1SAndroid Build Coastguard Worker template <size_t num_relocs, typename RelocType>
40*5225e6b1SAndroid Build Coastguard Worker class Program {
41*5225e6b1SAndroid Build Coastguard Worker public:
42*5225e6b1SAndroid Build Coastguard Worker struct DynamicSection {
43*5225e6b1SAndroid Build Coastguard Worker Elf64_Dyn symtab;
44*5225e6b1SAndroid Build Coastguard Worker Elf64_Dyn relx;
45*5225e6b1SAndroid Build Coastguard Worker Elf64_Dyn relx_size;
46*5225e6b1SAndroid Build Coastguard Worker Elf64_Dyn other_entry;
47*5225e6b1SAndroid Build Coastguard Worker Elf64_Dyn end;
48*5225e6b1SAndroid Build Coastguard Worker };
49*5225e6b1SAndroid Build Coastguard Worker
Program()50*5225e6b1SAndroid Build Coastguard Worker Program() {
51*5225e6b1SAndroid Build Coastguard Worker dynamic_section_ = {
52*5225e6b1SAndroid Build Coastguard Worker .symtab =
53*5225e6b1SAndroid Build Coastguard Worker {
54*5225e6b1SAndroid Build Coastguard Worker .d_tag = DT_SYMTAB,
55*5225e6b1SAndroid Build Coastguard Worker .d_un = {.d_ptr = offsetof(ThisType, symtab_)},
56*5225e6b1SAndroid Build Coastguard Worker },
57*5225e6b1SAndroid Build Coastguard Worker .relx =
58*5225e6b1SAndroid Build Coastguard Worker {
59*5225e6b1SAndroid Build Coastguard Worker .d_tag = DtTags<RelocType>::kTableTag,
60*5225e6b1SAndroid Build Coastguard Worker .d_un = {.d_ptr = offsetof(ThisType, table_)},
61*5225e6b1SAndroid Build Coastguard Worker },
62*5225e6b1SAndroid Build Coastguard Worker .relx_size =
63*5225e6b1SAndroid Build Coastguard Worker {
64*5225e6b1SAndroid Build Coastguard Worker .d_tag = DtTags<RelocType>::kTableSizeTag,
65*5225e6b1SAndroid Build Coastguard Worker .d_un = {.d_ptr = sizeof(table_)},
66*5225e6b1SAndroid Build Coastguard Worker },
67*5225e6b1SAndroid Build Coastguard Worker .other_entry =
68*5225e6b1SAndroid Build Coastguard Worker {
69*5225e6b1SAndroid Build Coastguard Worker .d_tag = DT_NULL,
70*5225e6b1SAndroid Build Coastguard Worker },
71*5225e6b1SAndroid Build Coastguard Worker .end =
72*5225e6b1SAndroid Build Coastguard Worker {
73*5225e6b1SAndroid Build Coastguard Worker .d_tag = DT_NULL,
74*5225e6b1SAndroid Build Coastguard Worker },
75*5225e6b1SAndroid Build Coastguard Worker };
76*5225e6b1SAndroid Build Coastguard Worker
77*5225e6b1SAndroid Build Coastguard Worker for (size_t i = 0; i < num_relocs; i++) {
78*5225e6b1SAndroid Build Coastguard Worker table_[i].r_offset = offsetof(ThisType, relocated_addr_table_) + i * sizeof(uint64_t);
79*5225e6b1SAndroid Build Coastguard Worker }
80*5225e6b1SAndroid Build Coastguard Worker }
81*5225e6b1SAndroid Build Coastguard Worker
GetRelocatedAddr(size_t index)82*5225e6b1SAndroid Build Coastguard Worker uint64_t GetRelocatedAddr(size_t index) { return relocated_addr_table_[index]; }
83*5225e6b1SAndroid Build Coastguard Worker
SetRelocationInfo(size_t index,uint64_t type,uint64_t addend)84*5225e6b1SAndroid Build Coastguard Worker void SetRelocationInfo(size_t index, uint64_t type, uint64_t addend) {
85*5225e6b1SAndroid Build Coastguard Worker ASSERT_LE(index, num_relocs);
86*5225e6b1SAndroid Build Coastguard Worker table_[index].r_info = type;
87*5225e6b1SAndroid Build Coastguard Worker SetAddend(table_[index], relocated_addr_table_[index], addend);
88*5225e6b1SAndroid Build Coastguard Worker }
89*5225e6b1SAndroid Build Coastguard Worker
DynamicSectionAddr()90*5225e6b1SAndroid Build Coastguard Worker uint64_t DynamicSectionAddr() { return reinterpret_cast<uint64_t>(&dynamic_section_); }
91*5225e6b1SAndroid Build Coastguard Worker
ProgramBase()92*5225e6b1SAndroid Build Coastguard Worker uint64_t ProgramBase() { return reinterpret_cast<uint64_t>(this); }
93*5225e6b1SAndroid Build Coastguard Worker
dynamic_section()94*5225e6b1SAndroid Build Coastguard Worker DynamicSection& dynamic_section() { return dynamic_section_; }
95*5225e6b1SAndroid Build Coastguard Worker
96*5225e6b1SAndroid Build Coastguard Worker public:
97*5225e6b1SAndroid Build Coastguard Worker using ThisType = Program<num_relocs, RelocType>;
98*5225e6b1SAndroid Build Coastguard Worker DynamicSection dynamic_section_;
99*5225e6b1SAndroid Build Coastguard Worker Elf64_Sym symtab_;
100*5225e6b1SAndroid Build Coastguard Worker RelocType table_[num_relocs];
101*5225e6b1SAndroid Build Coastguard Worker uint64_t relocated_addr_table_[num_relocs];
102*5225e6b1SAndroid Build Coastguard Worker
SetAddend(Elf64_Rela & entry,uint64_t &,uint64_t addend)103*5225e6b1SAndroid Build Coastguard Worker void SetAddend(Elf64_Rela& entry, uint64_t&, uint64_t addend) { entry.r_addend = addend; }
104*5225e6b1SAndroid Build Coastguard Worker
SetAddend(Elf64_Rel &,uint64_t & relocated_addr,uint64_t addend)105*5225e6b1SAndroid Build Coastguard Worker void SetAddend(Elf64_Rel&, uint64_t& relocated_addr, uint64_t addend) { relocated_addr = addend; }
106*5225e6b1SAndroid Build Coastguard Worker };
107*5225e6b1SAndroid Build Coastguard Worker
108*5225e6b1SAndroid Build Coastguard Worker template <typename T>
TestRelxRelocation()109*5225e6b1SAndroid Build Coastguard Worker void TestRelxRelocation() {
110*5225e6b1SAndroid Build Coastguard Worker Program<2, T> program;
111*5225e6b1SAndroid Build Coastguard Worker
112*5225e6b1SAndroid Build Coastguard Worker const uint64_t kAddend_0 = 0x100;
113*5225e6b1SAndroid Build Coastguard Worker program.SetRelocationInfo(0, R_RISCV_RELATIVE, kAddend_0);
114*5225e6b1SAndroid Build Coastguard Worker const uint64_t kAddend_1 = 0x200;
115*5225e6b1SAndroid Build Coastguard Worker program.SetRelocationInfo(1, R_RISCV_RELATIVE, kAddend_1);
116*5225e6b1SAndroid Build Coastguard Worker
117*5225e6b1SAndroid Build Coastguard Worker EXPECT_TRUE(ApplyRelocation(program.ProgramBase(), program.DynamicSectionAddr()));
118*5225e6b1SAndroid Build Coastguard Worker
119*5225e6b1SAndroid Build Coastguard Worker EXPECT_EQ(program.GetRelocatedAddr(0), program.ProgramBase() + kAddend_0);
120*5225e6b1SAndroid Build Coastguard Worker EXPECT_EQ(program.GetRelocatedAddr(1), program.ProgramBase() + kAddend_1);
121*5225e6b1SAndroid Build Coastguard Worker }
122*5225e6b1SAndroid Build Coastguard Worker
TEST(RelocationTest,RelaRelocation)123*5225e6b1SAndroid Build Coastguard Worker TEST(RelocationTest, RelaRelocation) {
124*5225e6b1SAndroid Build Coastguard Worker TestRelxRelocation<Elf64_Rela>();
125*5225e6b1SAndroid Build Coastguard Worker }
126*5225e6b1SAndroid Build Coastguard Worker
TEST(RelocationTest,RelRelocation)127*5225e6b1SAndroid Build Coastguard Worker TEST(RelocationTest, RelRelocation) {
128*5225e6b1SAndroid Build Coastguard Worker TestRelxRelocation<Elf64_Rel>();
129*5225e6b1SAndroid Build Coastguard Worker }
130*5225e6b1SAndroid Build Coastguard Worker
131*5225e6b1SAndroid Build Coastguard Worker template <typename T>
TestUnrecognizeRelcoationType()132*5225e6b1SAndroid Build Coastguard Worker void TestUnrecognizeRelcoationType() {
133*5225e6b1SAndroid Build Coastguard Worker Program<1, T> program;
134*5225e6b1SAndroid Build Coastguard Worker program.SetRelocationInfo(0, R_RISCV_RELATIVE + 1, 0x1000);
135*5225e6b1SAndroid Build Coastguard Worker EXPECT_FALSE(ApplyRelocation(program.ProgramBase(), program.DynamicSectionAddr()));
136*5225e6b1SAndroid Build Coastguard Worker }
137*5225e6b1SAndroid Build Coastguard Worker
TEST(RelocationTest,RelaUnrecognizeRelcoationType)138*5225e6b1SAndroid Build Coastguard Worker TEST(RelocationTest, RelaUnrecognizeRelcoationType) {
139*5225e6b1SAndroid Build Coastguard Worker TestUnrecognizeRelcoationType<Elf64_Rela>();
140*5225e6b1SAndroid Build Coastguard Worker }
141*5225e6b1SAndroid Build Coastguard Worker
TEST(RelocationTest,RelUnrecognizeRelcoationType)142*5225e6b1SAndroid Build Coastguard Worker TEST(RelocationTest, RelUnrecognizeRelcoationType) {
143*5225e6b1SAndroid Build Coastguard Worker TestUnrecognizeRelcoationType<Elf64_Rel>();
144*5225e6b1SAndroid Build Coastguard Worker }
145*5225e6b1SAndroid Build Coastguard Worker
146*5225e6b1SAndroid Build Coastguard Worker template <typename T>
TestRelxIncompleteInformation()147*5225e6b1SAndroid Build Coastguard Worker void TestRelxIncompleteInformation() {
148*5225e6b1SAndroid Build Coastguard Worker {
149*5225e6b1SAndroid Build Coastguard Worker Program<1, T> program;
150*5225e6b1SAndroid Build Coastguard Worker program.SetRelocationInfo(0, R_RISCV_RELATIVE, 0x1000);
151*5225e6b1SAndroid Build Coastguard Worker // Missing table size.
152*5225e6b1SAndroid Build Coastguard Worker program.dynamic_section().relx_size.d_tag = DT_HASH; // Don't care
153*5225e6b1SAndroid Build Coastguard Worker EXPECT_FALSE(ApplyRelocation(program.ProgramBase(), program.DynamicSectionAddr()));
154*5225e6b1SAndroid Build Coastguard Worker }
155*5225e6b1SAndroid Build Coastguard Worker
156*5225e6b1SAndroid Build Coastguard Worker {
157*5225e6b1SAndroid Build Coastguard Worker Program<1, T> program;
158*5225e6b1SAndroid Build Coastguard Worker program.SetRelocationInfo(0, R_RISCV_RELATIVE, 0x1000);
159*5225e6b1SAndroid Build Coastguard Worker // Missing table itself.
160*5225e6b1SAndroid Build Coastguard Worker program.dynamic_section().relx.d_tag = DT_HASH; // Don't care
161*5225e6b1SAndroid Build Coastguard Worker EXPECT_FALSE(ApplyRelocation(program.ProgramBase(), program.DynamicSectionAddr()));
162*5225e6b1SAndroid Build Coastguard Worker }
163*5225e6b1SAndroid Build Coastguard Worker }
164*5225e6b1SAndroid Build Coastguard Worker
TEST(RelocationTest,TestRelaIncompleteInformation)165*5225e6b1SAndroid Build Coastguard Worker TEST(RelocationTest, TestRelaIncompleteInformation) {
166*5225e6b1SAndroid Build Coastguard Worker TestRelxIncompleteInformation<Elf64_Rela>();
167*5225e6b1SAndroid Build Coastguard Worker }
168*5225e6b1SAndroid Build Coastguard Worker
TEST(RelocationTest,TestRelIncompleteInformation)169*5225e6b1SAndroid Build Coastguard Worker TEST(RelocationTest, TestRelIncompleteInformation) {
170*5225e6b1SAndroid Build Coastguard Worker TestRelxIncompleteInformation<Elf64_Rel>();
171*5225e6b1SAndroid Build Coastguard Worker }
172*5225e6b1SAndroid Build Coastguard Worker
TEST(RelocationTest,PltUnsupported)173*5225e6b1SAndroid Build Coastguard Worker TEST(RelocationTest, PltUnsupported) {
174*5225e6b1SAndroid Build Coastguard Worker Program<1, Elf64_Rela> program;
175*5225e6b1SAndroid Build Coastguard Worker program.SetRelocationInfo(0, R_RISCV_RELATIVE, 0x1000);
176*5225e6b1SAndroid Build Coastguard Worker program.dynamic_section().other_entry.d_tag = DT_JMPREL;
177*5225e6b1SAndroid Build Coastguard Worker EXPECT_FALSE(ApplyRelocation(program.ProgramBase(), program.DynamicSectionAddr()));
178*5225e6b1SAndroid Build Coastguard Worker }
179*5225e6b1SAndroid Build Coastguard Worker
TEST(RelocationTest,RelrUnsupported)180*5225e6b1SAndroid Build Coastguard Worker TEST(RelocationTest, RelrUnsupported) {
181*5225e6b1SAndroid Build Coastguard Worker Program<1, Elf64_Rela> program;
182*5225e6b1SAndroid Build Coastguard Worker program.SetRelocationInfo(0, R_RISCV_RELATIVE, 0x1000);
183*5225e6b1SAndroid Build Coastguard Worker program.dynamic_section().other_entry.d_tag = DT_RELR;
184*5225e6b1SAndroid Build Coastguard Worker EXPECT_FALSE(ApplyRelocation(program.ProgramBase(), program.DynamicSectionAddr()));
185*5225e6b1SAndroid Build Coastguard Worker }
186*5225e6b1SAndroid Build Coastguard Worker
TEST(RelocationTest,MissingSymtab)187*5225e6b1SAndroid Build Coastguard Worker TEST(RelocationTest, MissingSymtab) {
188*5225e6b1SAndroid Build Coastguard Worker Program<1, Elf64_Rela> program;
189*5225e6b1SAndroid Build Coastguard Worker program.SetRelocationInfo(0, R_RISCV_RELATIVE, 0x1000);
190*5225e6b1SAndroid Build Coastguard Worker program.dynamic_section().symtab.d_tag = DT_HASH; // Don't care
191*5225e6b1SAndroid Build Coastguard Worker EXPECT_FALSE(ApplyRelocation(program.ProgramBase(), program.DynamicSectionAddr()));
192*5225e6b1SAndroid Build Coastguard Worker }
193