|
1 | 1 | #include "event_object.h"
|
2 | 2 |
|
| 3 | +#include <fstream> |
| 4 | + |
3 | 5 | #include <iomanip>
|
4 | 6 | #include <algorithm>
|
5 | 7 |
|
6 |
| -#include "../ea/event_section.h" |
| 8 | +#include "elfcpp/elfcpp_file.h" |
| 9 | + |
| 10 | +#include "ea/event_section.h" |
7 | 11 | #include "util/hex_write.h"
|
8 | 12 |
|
| 13 | +namespace elfcpp { |
| 14 | + |
| 15 | +// for some reason this is required in order to make the whole thing work |
| 16 | + |
| 17 | +template<int Size, bool BigEndian, typename File> |
| 18 | +const int Elf_file<Size, BigEndian, File>::ehdr_size; |
| 19 | + |
| 20 | +template<int Size, bool BigEndian, typename File> |
| 21 | +const int Elf_file<Size, BigEndian, File>::phdr_size; |
| 22 | + |
| 23 | +template<int Size, bool BigEndian, typename File> |
| 24 | +const int Elf_file<Size, BigEndian, File>::shdr_size; |
| 25 | + |
| 26 | +template<int Size, bool BigEndian, typename File> |
| 27 | +const int Elf_file<Size, BigEndian, File>::sym_size; |
| 28 | + |
| 29 | +template<int Size, bool BigEndian, typename File> |
| 30 | +const int Elf_file<Size, BigEndian, File>::rel_size; |
| 31 | + |
| 32 | +template<int Size, bool BigEndian, typename File> |
| 33 | +const int Elf_file<Size, BigEndian, File>::rela_size; |
| 34 | + |
| 35 | +} // namespace elfcpp |
| 36 | + |
9 | 37 | namespace lyn {
|
10 | 38 |
|
| 39 | +void event_object::append_from_elf(const char* fileName) { |
| 40 | + binary_file file; { |
| 41 | + std::ifstream fileStream; |
| 42 | + |
| 43 | + fileStream.open(fileName, std::ios::in | std::ios::binary); |
| 44 | + |
| 45 | + if (!fileStream.is_open()) |
| 46 | + throw std::runtime_error(std::string("Couldn't open file for read: ").append(fileName)); // TODO: better error |
| 47 | + |
| 48 | + file.load_from_stream(fileStream); |
| 49 | + fileStream.close(); |
| 50 | + } |
| 51 | + |
| 52 | + elfcpp::Elf_file<32, false, lyn::binary_file> elfFile(&file); |
| 53 | + |
| 54 | + auto readString = [&file] (elfcpp::Shdr<32, false> section, unsigned offset) -> std::string { |
| 55 | + return file.cstr_at(section.get_sh_offset() + offset); |
| 56 | + }; |
| 57 | + |
| 58 | + std::vector<section_data> newSections(elfFile.shnum()); |
| 59 | + std::vector<bool> outMap(elfFile.shnum(), false); |
| 60 | + |
| 61 | + auto getLocalSymbolName = [this] (int section, int index) -> std::string { |
| 62 | + std::string result; |
| 63 | + result.reserve(4 + 8 + 4 + 4); |
| 64 | + |
| 65 | + result.append("_L"); |
| 66 | + |
| 67 | + util::append_hex(result, size()); |
| 68 | + result.append("_"); |
| 69 | + util::append_hex(result, section); |
| 70 | + result.append("_"); |
| 71 | + util::append_hex(result, index); |
| 72 | + |
| 73 | + return result; |
| 74 | + }; |
| 75 | + |
| 76 | + auto getGlobalSymbolName = [this] (const char* name) -> std::string { |
| 77 | + std::string result(name); |
| 78 | + int find = 0; |
| 79 | + |
| 80 | + while ((find = result.find('.')) != std::string::npos) |
| 81 | + result[find] = '_'; |
| 82 | + |
| 83 | + return result; |
| 84 | + }; |
| 85 | + |
| 86 | + for (unsigned i = 0; i < elfFile.shnum(); ++i) { |
| 87 | + auto flags = elfFile.section_flags(i); |
| 88 | + |
| 89 | + if ((flags & elfcpp::SHF_ALLOC) && !(flags & elfcpp::SHF_WRITE)) { |
| 90 | + auto& section = newSections.at(i); |
| 91 | + auto loc = elfFile.section_contents(i); |
| 92 | + |
| 93 | + section.set_name(elfFile.section_name(i)); |
| 94 | + section.load_from_other(file, loc.file_offset, loc.data_size); |
| 95 | + |
| 96 | + outMap[i] = true; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + for (unsigned si = 0; si < elfFile.shnum(); ++si) { |
| 101 | + elfcpp::Shdr<32, false> header(&file, elfFile.section_header(si)); |
| 102 | + |
| 103 | + switch (header.get_sh_type()) { |
| 104 | + |
| 105 | + case elfcpp::SHT_SYMTAB: { |
| 106 | + const unsigned count = header.get_sh_size() / header.get_sh_entsize(); |
| 107 | + const elfcpp::Shdr<32, false> nameShdr(&file, elfFile.section_header(header.get_sh_link())); |
| 108 | + |
| 109 | + for (unsigned i = 0; i < count; ++i) { |
| 110 | + elfcpp::Sym<32, false> sym(&file, binary_file::Location( |
| 111 | + header.get_sh_offset() + i * header.get_sh_entsize(), |
| 112 | + header.get_sh_entsize() |
| 113 | + )); |
| 114 | + |
| 115 | + switch (sym.get_st_shndx()) { |
| 116 | + |
| 117 | + case elfcpp::SHN_ABS: { |
| 118 | + if (sym.get_st_bind() != elfcpp::STB_GLOBAL) |
| 119 | + break; |
| 120 | + |
| 121 | + mAbsoluteSymbols.push_back(symbol { |
| 122 | + readString(nameShdr, sym.get_st_name()), |
| 123 | + sym.get_st_value(), |
| 124 | + false |
| 125 | + }); |
| 126 | + |
| 127 | + break; |
| 128 | + } // case elfcpp::SHN_ABS |
| 129 | + |
| 130 | + default: { |
| 131 | + if (!outMap.at(sym.get_st_shndx())) |
| 132 | + break; |
| 133 | + |
| 134 | + auto& section = newSections.at(sym.get_st_shndx()); |
| 135 | + |
| 136 | + std::string name = readString(nameShdr, sym.get_st_name()); |
| 137 | + |
| 138 | + if (sym.get_st_type() == elfcpp::STT_NOTYPE && sym.get_st_bind() == elfcpp::STB_LOCAL) { |
| 139 | + std::string subString = name.substr(0, 3); |
| 140 | + |
| 141 | + if ((name == "$t") || (subString == "$t.")) { |
| 142 | + section.set_mapping(sym.get_st_value(), mapping::Thumb); |
| 143 | + break; |
| 144 | + } else if ((name == "$a") || (subString == "$a.")) { |
| 145 | + section.set_mapping(sym.get_st_value(), mapping::ARM); |
| 146 | + break; |
| 147 | + } else if ((name == "$d") || (subString == "$d.")) { |
| 148 | + section.set_mapping(sym.get_st_value(), mapping::Data); |
| 149 | + break; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + if (sym.get_st_bind() == elfcpp::STB_LOCAL) |
| 154 | + name = getLocalSymbolName(si, i); |
| 155 | + else |
| 156 | + name = getGlobalSymbolName(name.c_str()); |
| 157 | + |
| 158 | + section.symbols().push_back(symbol { |
| 159 | + name, |
| 160 | + sym.get_st_value(), |
| 161 | + (sym.get_st_bind() == elfcpp::STB_LOCAL) |
| 162 | + }); |
| 163 | + |
| 164 | + break; |
| 165 | + } // default |
| 166 | + |
| 167 | + } // switch (sym.get_st_shndx()) |
| 168 | + } |
| 169 | + |
| 170 | + break; |
| 171 | + } // case elfcpp::SHT_SYMTAB |
| 172 | + |
| 173 | + case elfcpp::SHT_REL: { |
| 174 | + if (!outMap.at(header.get_sh_info())) |
| 175 | + break; |
| 176 | + |
| 177 | + const unsigned count = header.get_sh_size() / header.get_sh_entsize(); |
| 178 | + |
| 179 | + const elfcpp::Shdr<32, false> symShdr(&file, elfFile.section_header(header.get_sh_link())); |
| 180 | + const elfcpp::Shdr<32, false> symNameShdr(&file, elfFile.section_header(symShdr.get_sh_link())); |
| 181 | + |
| 182 | + auto& section = newSections.at(header.get_sh_info()); |
| 183 | + |
| 184 | + for (unsigned i = 0; i < count; ++i) { |
| 185 | + const elfcpp::Rel<32, false> rel(&file, binary_file::Location( |
| 186 | + header.get_sh_offset() + i * header.get_sh_entsize(), |
| 187 | + header.get_sh_entsize() |
| 188 | + )); |
| 189 | + |
| 190 | + const elfcpp::Sym<32, false> sym(&file, binary_file::Location( |
| 191 | + symShdr.get_sh_offset() + elfcpp::elf_r_sym<32>(rel.get_r_info()) * symShdr.get_sh_entsize(), |
| 192 | + symShdr.get_sh_entsize() |
| 193 | + )); |
| 194 | + |
| 195 | + const std::string name = (sym.get_st_bind() == elfcpp::STB_LOCAL) |
| 196 | + ? getLocalSymbolName(header.get_sh_link(), elfcpp::elf_r_sym<32>(rel.get_r_info())) |
| 197 | + : getGlobalSymbolName(readString(symNameShdr, sym.get_st_name()).c_str()); |
| 198 | + |
| 199 | + section.relocations().push_back(relocation { |
| 200 | + name, |
| 201 | + 0, |
| 202 | + elfcpp::elf_r_type<32>(rel.get_r_info()), |
| 203 | + rel.get_r_offset() |
| 204 | + }); |
| 205 | + } |
| 206 | + |
| 207 | + break; |
| 208 | + } // case elfcpp::SHT_REL |
| 209 | + |
| 210 | + case elfcpp::SHT_RELA: { |
| 211 | + if (!outMap.at(header.get_sh_info())) |
| 212 | + break; |
| 213 | + |
| 214 | + const unsigned count = header.get_sh_size() / header.get_sh_entsize(); |
| 215 | + |
| 216 | + const elfcpp::Shdr<32, false> symShdr(&file, elfFile.section_header(header.get_sh_link())); |
| 217 | + const elfcpp::Shdr<32, false> symNameShdr(&file, elfFile.section_header(symShdr.get_sh_link())); |
| 218 | + |
| 219 | + auto& section = newSections.at(header.get_sh_info()); |
| 220 | + |
| 221 | + for (unsigned i = 0; i < count; ++i) { |
| 222 | + const elfcpp::Rela<32, false> rela(&file, binary_file::Location( |
| 223 | + header.get_sh_offset() + i * header.get_sh_entsize(), |
| 224 | + header.get_sh_entsize() |
| 225 | + )); |
| 226 | + |
| 227 | + const elfcpp::Sym<32, false> sym(&file, binary_file::Location( |
| 228 | + symShdr.get_sh_offset() + elfcpp::elf_r_sym<32>(rela.get_r_info()) * symShdr.get_sh_entsize(), |
| 229 | + symShdr.get_sh_entsize() |
| 230 | + )); |
| 231 | + |
| 232 | + const std::string name = (sym.get_st_bind() == elfcpp::STB_LOCAL) |
| 233 | + ? getLocalSymbolName(header.get_sh_link(), elfcpp::elf_r_sym<32>(rela.get_r_info())) |
| 234 | + : getGlobalSymbolName(readString(symNameShdr, sym.get_st_name()).c_str()); |
| 235 | + |
| 236 | + section.relocations().push_back(relocation { |
| 237 | + name, |
| 238 | + rela.get_r_addend(), |
| 239 | + elfcpp::elf_r_type<32>(rela.get_r_info()), |
| 240 | + rela.get_r_offset() |
| 241 | + }); |
| 242 | + } |
| 243 | + |
| 244 | + break; |
| 245 | + } // case elfcpp::SHT_RELA |
| 246 | + |
| 247 | + } // switch (header.get_sh_type()) |
| 248 | + } |
| 249 | + |
| 250 | + // Remove empty sections |
| 251 | + |
| 252 | + newSections.erase(std::remove_if(newSections.begin(), newSections.end(), [] (const section_data& section) { |
| 253 | + return (section.size()==0); |
| 254 | + }), newSections.end()); |
| 255 | + |
| 256 | + // Create the ultimate lifeform |
| 257 | + |
| 258 | + for (auto& section : newSections) { |
| 259 | + combine_with(std::move(section)); |
| 260 | + ensure_aligned(4); |
| 261 | + } |
| 262 | +} |
| 263 | + |
11 | 264 | void event_object::append_from_elf(const elf_file& elfFile) {
|
12 | 265 | std::vector<section_data> newSections(elfFile.sections().size());
|
13 | 266 | std::vector<bool> outMap(elfFile.sections().size(), false);
|
|
0 commit comments