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