Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1 | //===-- ObjectFileELF.cpp ------------------------------------- -*- C++ -*-===// |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "ObjectFileELF.h" |
| 11 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 12 | #include <cassert> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 15 | #include "lldb/Core/ArchSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | #include "lldb/Core/DataBuffer.h" |
| 17 | #include "lldb/Core/Error.h" |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 18 | #include "lldb/Core/FileSpecList.h" |
Ed Maste | c113ff8 | 2013-12-02 17:49:13 +0000 | [diff] [blame] | 19 | #include "lldb/Core/Log.h" |
Jim Ingham | 672e6f5 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 20 | #include "lldb/Core/Module.h" |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 21 | #include "lldb/Core/ModuleSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | #include "lldb/Core/PluginManager.h" |
| 23 | #include "lldb/Core/Section.h" |
| 24 | #include "lldb/Core/Stream.h" |
Ashok Thirumurthi | 35729bb | 2013-09-24 15:34:13 +0000 | [diff] [blame] | 25 | #include "lldb/Symbol/DWARFCallFrameInfo.h" |
Jim Ingham | 672e6f5 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 26 | #include "lldb/Symbol/SymbolContext.h" |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 27 | #include "lldb/Target/SectionLoadList.h" |
Ed Maste | 5480365 | 2013-10-11 17:39:07 +0000 | [diff] [blame] | 28 | #include "lldb/Target/Target.h" |
Greg Clayton | 64195a2 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 29 | #include "lldb/Host/Host.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/PointerUnion.h" |
| 32 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 33 | #define CASE_AND_STREAM(s, def, width) \ |
| 34 | case def: s->Printf("%-*s", width, #def); break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | using namespace lldb; |
| 37 | using namespace lldb_private; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 38 | using namespace elf; |
| 39 | using namespace llvm::ELF; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 41 | namespace { |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | /// @class ELFRelocation |
| 44 | /// @brief Generic wrapper for ELFRel and ELFRela. |
| 45 | /// |
| 46 | /// This helper class allows us to parse both ELFRel and ELFRela relocation |
| 47 | /// entries in a generic manner. |
| 48 | class ELFRelocation |
| 49 | { |
| 50 | public: |
| 51 | |
| 52 | /// Constructs an ELFRelocation entry with a personality as given by @p |
| 53 | /// type. |
| 54 | /// |
| 55 | /// @param type Either DT_REL or DT_RELA. Any other value is invalid. |
| 56 | ELFRelocation(unsigned type); |
| 57 | |
| 58 | ~ELFRelocation(); |
| 59 | |
| 60 | bool |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 61 | Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 62 | |
| 63 | static unsigned |
| 64 | RelocType32(const ELFRelocation &rel); |
| 65 | |
| 66 | static unsigned |
| 67 | RelocType64(const ELFRelocation &rel); |
| 68 | |
| 69 | static unsigned |
| 70 | RelocSymbol32(const ELFRelocation &rel); |
| 71 | |
| 72 | static unsigned |
| 73 | RelocSymbol64(const ELFRelocation &rel); |
| 74 | |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 75 | static unsigned |
| 76 | RelocOffset32(const ELFRelocation &rel); |
| 77 | |
| 78 | static unsigned |
| 79 | RelocOffset64(const ELFRelocation &rel); |
| 80 | |
| 81 | static unsigned |
| 82 | RelocAddend32(const ELFRelocation &rel); |
| 83 | |
| 84 | static unsigned |
| 85 | RelocAddend64(const ELFRelocation &rel); |
| 86 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 87 | private: |
| 88 | typedef llvm::PointerUnion<ELFRel*, ELFRela*> RelocUnion; |
| 89 | |
| 90 | RelocUnion reloc; |
| 91 | }; |
| 92 | |
| 93 | ELFRelocation::ELFRelocation(unsigned type) |
| 94 | { |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 95 | if (type == DT_REL || type == SHT_REL) |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 96 | reloc = new ELFRel(); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 97 | else if (type == DT_RELA || type == SHT_RELA) |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 98 | reloc = new ELFRela(); |
| 99 | else { |
| 100 | assert(false && "unexpected relocation type"); |
| 101 | reloc = static_cast<ELFRel*>(NULL); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | ELFRelocation::~ELFRelocation() |
| 106 | { |
| 107 | if (reloc.is<ELFRel*>()) |
| 108 | delete reloc.get<ELFRel*>(); |
| 109 | else |
| 110 | delete reloc.get<ELFRela*>(); |
| 111 | } |
| 112 | |
| 113 | bool |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 114 | ELFRelocation::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset) |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 115 | { |
| 116 | if (reloc.is<ELFRel*>()) |
| 117 | return reloc.get<ELFRel*>()->Parse(data, offset); |
| 118 | else |
| 119 | return reloc.get<ELFRela*>()->Parse(data, offset); |
| 120 | } |
| 121 | |
| 122 | unsigned |
| 123 | ELFRelocation::RelocType32(const ELFRelocation &rel) |
| 124 | { |
| 125 | if (rel.reloc.is<ELFRel*>()) |
| 126 | return ELFRel::RelocType32(*rel.reloc.get<ELFRel*>()); |
| 127 | else |
| 128 | return ELFRela::RelocType32(*rel.reloc.get<ELFRela*>()); |
| 129 | } |
| 130 | |
| 131 | unsigned |
| 132 | ELFRelocation::RelocType64(const ELFRelocation &rel) |
| 133 | { |
| 134 | if (rel.reloc.is<ELFRel*>()) |
| 135 | return ELFRel::RelocType64(*rel.reloc.get<ELFRel*>()); |
| 136 | else |
| 137 | return ELFRela::RelocType64(*rel.reloc.get<ELFRela*>()); |
| 138 | } |
| 139 | |
| 140 | unsigned |
| 141 | ELFRelocation::RelocSymbol32(const ELFRelocation &rel) |
| 142 | { |
| 143 | if (rel.reloc.is<ELFRel*>()) |
| 144 | return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel*>()); |
| 145 | else |
| 146 | return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela*>()); |
| 147 | } |
| 148 | |
| 149 | unsigned |
| 150 | ELFRelocation::RelocSymbol64(const ELFRelocation &rel) |
| 151 | { |
| 152 | if (rel.reloc.is<ELFRel*>()) |
| 153 | return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel*>()); |
| 154 | else |
| 155 | return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela*>()); |
| 156 | } |
| 157 | |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 158 | unsigned |
| 159 | ELFRelocation::RelocOffset32(const ELFRelocation &rel) |
| 160 | { |
| 161 | if (rel.reloc.is<ELFRel*>()) |
| 162 | return rel.reloc.get<ELFRel*>()->r_offset; |
| 163 | else |
| 164 | return rel.reloc.get<ELFRela*>()->r_offset; |
| 165 | } |
| 166 | |
| 167 | unsigned |
| 168 | ELFRelocation::RelocOffset64(const ELFRelocation &rel) |
| 169 | { |
| 170 | if (rel.reloc.is<ELFRel*>()) |
| 171 | return rel.reloc.get<ELFRel*>()->r_offset; |
| 172 | else |
| 173 | return rel.reloc.get<ELFRela*>()->r_offset; |
| 174 | } |
| 175 | |
| 176 | unsigned |
| 177 | ELFRelocation::RelocAddend32(const ELFRelocation &rel) |
| 178 | { |
| 179 | if (rel.reloc.is<ELFRel*>()) |
| 180 | return 0; |
| 181 | else |
| 182 | return rel.reloc.get<ELFRela*>()->r_addend; |
| 183 | } |
| 184 | |
| 185 | unsigned |
| 186 | ELFRelocation::RelocAddend64(const ELFRelocation &rel) |
| 187 | { |
| 188 | if (rel.reloc.is<ELFRel*>()) |
| 189 | return 0; |
| 190 | else |
| 191 | return rel.reloc.get<ELFRela*>()->r_addend; |
| 192 | } |
| 193 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 194 | } // end anonymous namespace |
| 195 | |
Ed Maste | c113ff8 | 2013-12-02 17:49:13 +0000 | [diff] [blame] | 196 | bool |
| 197 | ELFNote::Parse(const DataExtractor &data, lldb::offset_t *offset) |
| 198 | { |
| 199 | // Read all fields. |
| 200 | if (data.GetU32(offset, &n_namesz, 3) == NULL) |
| 201 | return false; |
| 202 | |
| 203 | // The name field is required to be nul-terminated, and n_namesz |
| 204 | // includes the terminating nul in observed implementations (contrary |
| 205 | // to the ELF-64 spec). A special case is needed for cores generated |
| 206 | // by some older Linux versions, which write a note named "CORE" |
| 207 | // without a nul terminator and n_namesz = 4. |
| 208 | if (n_namesz == 4) |
| 209 | { |
| 210 | char buf[4]; |
| 211 | if (data.ExtractBytes (*offset, 4, data.GetByteOrder(), buf) != 4) |
| 212 | return false; |
| 213 | if (strncmp (buf, "CORE", 4) == 0) |
| 214 | { |
| 215 | n_name = "CORE"; |
| 216 | *offset += 4; |
| 217 | return true; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | const char *cstr = data.GetCStr(offset, llvm::RoundUpToAlignment (n_namesz, 4)); |
| 222 | if (cstr == NULL) |
| 223 | { |
| 224 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SYMBOLS)); |
| 225 | if (log) |
| 226 | log->Printf("Failed to parse note name lacking nul terminator"); |
| 227 | |
| 228 | return false; |
| 229 | } |
| 230 | n_name = cstr; |
| 231 | return true; |
| 232 | } |
| 233 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 234 | //------------------------------------------------------------------ |
| 235 | // Static methods. |
| 236 | //------------------------------------------------------------------ |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 237 | void |
| 238 | ObjectFileELF::Initialize() |
| 239 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 240 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 241 | GetPluginDescriptionStatic(), |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 242 | CreateInstance, |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 243 | CreateMemoryInstance, |
| 244 | GetModuleSpecifications); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void |
| 248 | ObjectFileELF::Terminate() |
| 249 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 250 | PluginManager::UnregisterPlugin(CreateInstance); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 253 | lldb_private::ConstString |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 254 | ObjectFileELF::GetPluginNameStatic() |
| 255 | { |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 256 | static ConstString g_name("elf"); |
| 257 | return g_name; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | const char * |
| 261 | ObjectFileELF::GetPluginDescriptionStatic() |
| 262 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 263 | return "ELF object file reader."; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 266 | ObjectFile * |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 267 | ObjectFileELF::CreateInstance (const lldb::ModuleSP &module_sp, |
| 268 | DataBufferSP &data_sp, |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 269 | lldb::offset_t data_offset, |
| 270 | const lldb_private::FileSpec* file, |
| 271 | lldb::offset_t file_offset, |
| 272 | lldb::offset_t length) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 273 | { |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 274 | if (!data_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 275 | { |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 276 | data_sp = file->MemoryMapFileContents(file_offset, length); |
| 277 | data_offset = 0; |
| 278 | } |
| 279 | |
| 280 | if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset)) |
| 281 | { |
| 282 | const uint8_t *magic = data_sp->GetBytes() + data_offset; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 283 | if (ELFHeader::MagicBytesMatch(magic)) |
| 284 | { |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 285 | // Update the data to contain the entire file if it doesn't already |
Andrew Kaylor | 213f672 | 2013-02-07 21:30:54 +0000 | [diff] [blame] | 286 | if (data_sp->GetByteSize() < length) { |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 287 | data_sp = file->MemoryMapFileContents(file_offset, length); |
Greg Clayton | 64ff6c7 | 2013-02-07 21:49:54 +0000 | [diff] [blame] | 288 | data_offset = 0; |
| 289 | magic = data_sp->GetBytes(); |
Andrew Kaylor | 213f672 | 2013-02-07 21:30:54 +0000 | [diff] [blame] | 290 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 291 | unsigned address_size = ELFHeader::AddressSizeInBytes(magic); |
| 292 | if (address_size == 4 || address_size == 8) |
| 293 | { |
Greg Clayton | 7b0992d | 2013-04-18 22:45:39 +0000 | [diff] [blame] | 294 | std::unique_ptr<ObjectFileELF> objfile_ap(new ObjectFileELF(module_sp, data_sp, data_offset, file, file_offset, length)); |
Stephen Wilson | 3f4200fd | 2011-02-24 19:16:15 +0000 | [diff] [blame] | 295 | ArchSpec spec; |
| 296 | if (objfile_ap->GetArchitecture(spec) && |
| 297 | objfile_ap->SetModulesArchitecture(spec)) |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 298 | return objfile_ap.release(); |
| 299 | } |
| 300 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 301 | } |
| 302 | return NULL; |
| 303 | } |
| 304 | |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 305 | |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 306 | ObjectFile* |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 307 | ObjectFileELF::CreateMemoryInstance (const lldb::ModuleSP &module_sp, |
| 308 | DataBufferSP& data_sp, |
| 309 | const lldb::ProcessSP &process_sp, |
| 310 | lldb::addr_t header_addr) |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 311 | { |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 312 | if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT)) |
| 313 | { |
| 314 | const uint8_t *magic = data_sp->GetBytes(); |
| 315 | if (ELFHeader::MagicBytesMatch(magic)) |
| 316 | { |
| 317 | unsigned address_size = ELFHeader::AddressSizeInBytes(magic); |
| 318 | if (address_size == 4 || address_size == 8) |
| 319 | { |
| 320 | std::auto_ptr<ObjectFileELF> objfile_ap(new ObjectFileELF(module_sp, data_sp, process_sp, header_addr)); |
| 321 | ArchSpec spec; |
| 322 | if (objfile_ap->GetArchitecture(spec) && |
| 323 | objfile_ap->SetModulesArchitecture(spec)) |
| 324 | return objfile_ap.release(); |
| 325 | } |
| 326 | } |
| 327 | } |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 328 | return NULL; |
| 329 | } |
| 330 | |
Michael Sartain | 9f0013d | 2013-05-17 00:20:21 +0000 | [diff] [blame] | 331 | bool |
| 332 | ObjectFileELF::MagicBytesMatch (DataBufferSP& data_sp, |
| 333 | lldb::addr_t data_offset, |
| 334 | lldb::addr_t data_length) |
| 335 | { |
| 336 | if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset)) |
| 337 | { |
| 338 | const uint8_t *magic = data_sp->GetBytes() + data_offset; |
| 339 | return ELFHeader::MagicBytesMatch(magic); |
| 340 | } |
| 341 | return false; |
| 342 | } |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 343 | |
Michael Sartain | 9f4517a | 2013-07-03 01:52:14 +0000 | [diff] [blame] | 344 | /* |
| 345 | * crc function from http://svnweb.freebsd.org/base/head/sys/libkern/crc32.c |
| 346 | * |
| 347 | * COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or |
| 348 | * code or tables extracted from it, as desired without restriction. |
| 349 | */ |
| 350 | static uint32_t |
| 351 | calc_gnu_debuglink_crc32(const void *buf, size_t size) |
| 352 | { |
| 353 | static const uint32_t g_crc32_tab[] = |
| 354 | { |
| 355 | 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, |
| 356 | 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, |
| 357 | 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, |
| 358 | 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, |
| 359 | 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, |
| 360 | 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, |
| 361 | 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, |
| 362 | 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, |
| 363 | 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, |
| 364 | 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, |
| 365 | 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, |
| 366 | 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, |
| 367 | 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, |
| 368 | 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, |
| 369 | 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, |
| 370 | 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, |
| 371 | 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, |
| 372 | 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, |
| 373 | 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, |
| 374 | 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, |
| 375 | 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, |
| 376 | 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, |
| 377 | 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, |
| 378 | 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, |
| 379 | 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, |
| 380 | 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, |
| 381 | 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, |
| 382 | 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, |
| 383 | 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, |
| 384 | 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, |
| 385 | 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, |
| 386 | 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, |
| 387 | 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, |
| 388 | 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, |
| 389 | 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, |
| 390 | 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, |
| 391 | 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, |
| 392 | 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, |
| 393 | 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, |
| 394 | 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, |
| 395 | 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, |
| 396 | 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, |
| 397 | 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d |
| 398 | }; |
| 399 | const uint8_t *p = (const uint8_t *)buf; |
| 400 | uint32_t crc; |
| 401 | |
| 402 | crc = ~0U; |
| 403 | while (size--) |
| 404 | crc = g_crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8); |
| 405 | return crc ^ ~0U; |
| 406 | } |
| 407 | |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 408 | size_t |
| 409 | ObjectFileELF::GetModuleSpecifications (const lldb_private::FileSpec& file, |
| 410 | lldb::DataBufferSP& data_sp, |
| 411 | lldb::offset_t data_offset, |
| 412 | lldb::offset_t file_offset, |
| 413 | lldb::offset_t length, |
| 414 | lldb_private::ModuleSpecList &specs) |
| 415 | { |
Michael Sartain | 9f0013d | 2013-05-17 00:20:21 +0000 | [diff] [blame] | 416 | const size_t initial_count = specs.GetSize(); |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 417 | |
Michael Sartain | 9f0013d | 2013-05-17 00:20:21 +0000 | [diff] [blame] | 418 | if (ObjectFileELF::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) |
| 419 | { |
| 420 | DataExtractor data; |
| 421 | data.SetData(data_sp); |
| 422 | elf::ELFHeader header; |
| 423 | if (header.Parse(data, &data_offset)) |
| 424 | { |
| 425 | if (data_sp) |
| 426 | { |
| 427 | ModuleSpec spec; |
| 428 | spec.GetFileSpec() = file; |
| 429 | spec.GetArchitecture().SetArchitecture(eArchTypeELF, |
| 430 | header.e_machine, |
| 431 | LLDB_INVALID_CPUTYPE); |
| 432 | if (spec.GetArchitecture().IsValid()) |
| 433 | { |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 434 | // We could parse the ABI tag information (in .note, .notes, or .note.ABI-tag) to get the |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 435 | // machine information. However, this info isn't guaranteed to exist or be correct. Details: |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 436 | // http://refspecs.linuxfoundation.org/LSB_1.2.0/gLSB/noteabitag.html |
| 437 | // Instead of passing potentially incorrect information down the pipeline, grab |
| 438 | // the host information and use it. |
| 439 | spec.GetArchitecture().GetTriple().setOSName (Host::GetOSString().GetCString()); |
| 440 | spec.GetArchitecture().GetTriple().setVendorName(Host::GetVendorString().GetCString()); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 441 | |
| 442 | // Try to get the UUID from the section list. Usually that's at the end, so |
| 443 | // map the file in if we don't have it already. |
| 444 | size_t section_header_end = header.e_shoff + header.e_shnum * header.e_shentsize; |
| 445 | if (section_header_end > data_sp->GetByteSize()) |
| 446 | { |
| 447 | data_sp = file.MemoryMapFileContents (file_offset, section_header_end); |
| 448 | data.SetData(data_sp); |
| 449 | } |
| 450 | |
Michael Sartain | 9f4517a | 2013-07-03 01:52:14 +0000 | [diff] [blame] | 451 | uint32_t gnu_debuglink_crc = 0; |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 452 | std::string gnu_debuglink_file; |
| 453 | SectionHeaderColl section_headers; |
Michael Sartain | 9f4517a | 2013-07-03 01:52:14 +0000 | [diff] [blame] | 454 | lldb_private::UUID &uuid = spec.GetUUID(); |
| 455 | GetSectionHeaderInfo(section_headers, data, header, uuid, gnu_debuglink_file, gnu_debuglink_crc); |
| 456 | |
| 457 | if (!uuid.IsValid()) |
| 458 | { |
| 459 | if (!gnu_debuglink_crc) |
| 460 | { |
| 461 | // Need to map entire file into memory to calculate the crc. |
| 462 | data_sp = file.MemoryMapFileContents (file_offset, SIZE_MAX); |
| 463 | data.SetData(data_sp); |
| 464 | gnu_debuglink_crc = calc_gnu_debuglink_crc32 (data.GetDataStart(), data.GetByteSize()); |
| 465 | } |
| 466 | if (gnu_debuglink_crc) |
| 467 | { |
| 468 | // Use 4 bytes of crc from the .gnu_debuglink section. |
| 469 | uint32_t uuidt[4] = { gnu_debuglink_crc, 0, 0, 0 }; |
| 470 | uuid.SetBytes (uuidt, sizeof(uuidt)); |
| 471 | } |
| 472 | } |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 473 | |
Michael Sartain | 9f0013d | 2013-05-17 00:20:21 +0000 | [diff] [blame] | 474 | specs.Append(spec); |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | } |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 479 | |
Michael Sartain | 9f0013d | 2013-05-17 00:20:21 +0000 | [diff] [blame] | 480 | return specs.GetSize() - initial_count; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 483 | //------------------------------------------------------------------ |
| 484 | // PluginInterface protocol |
| 485 | //------------------------------------------------------------------ |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 486 | lldb_private::ConstString |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 487 | ObjectFileELF::GetPluginName() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 488 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 489 | return GetPluginNameStatic(); |
| 490 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 491 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 492 | uint32_t |
| 493 | ObjectFileELF::GetPluginVersion() |
| 494 | { |
| 495 | return m_plugin_version; |
| 496 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 497 | //------------------------------------------------------------------ |
| 498 | // ObjectFile protocol |
| 499 | //------------------------------------------------------------------ |
| 500 | |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 501 | ObjectFileELF::ObjectFileELF (const lldb::ModuleSP &module_sp, |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 502 | DataBufferSP& data_sp, |
| 503 | lldb::offset_t data_offset, |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 504 | const FileSpec* file, |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 505 | lldb::offset_t file_offset, |
| 506 | lldb::offset_t length) : |
| 507 | ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset), |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 508 | m_header(), |
| 509 | m_program_headers(), |
| 510 | m_section_headers(), |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 511 | m_filespec_ap() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 512 | { |
| 513 | if (file) |
| 514 | m_file = *file; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 515 | ::memset(&m_header, 0, sizeof(m_header)); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 516 | m_gnu_debuglink_crc = 0; |
| 517 | m_gnu_debuglink_file.clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 520 | ObjectFileELF::ObjectFileELF (const lldb::ModuleSP &module_sp, |
| 521 | DataBufferSP& data_sp, |
| 522 | const lldb::ProcessSP &process_sp, |
| 523 | addr_t header_addr) : |
| 524 | ObjectFile(module_sp, process_sp, LLDB_INVALID_ADDRESS, data_sp), |
| 525 | m_header(), |
| 526 | m_program_headers(), |
| 527 | m_section_headers(), |
| 528 | m_filespec_ap() |
| 529 | { |
| 530 | ::memset(&m_header, 0, sizeof(m_header)); |
| 531 | } |
| 532 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 533 | ObjectFileELF::~ObjectFileELF() |
| 534 | { |
| 535 | } |
| 536 | |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 537 | bool |
| 538 | ObjectFileELF::IsExecutable() const |
| 539 | { |
Stephen Wilson | 7f3b57c | 2011-01-15 00:09:50 +0000 | [diff] [blame] | 540 | return m_header.e_entry != 0; |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 543 | bool |
Greg Clayton | 751caf6 | 2014-02-07 22:54:47 +0000 | [diff] [blame] | 544 | ObjectFileELF::SetLoadAddress (Target &target, |
| 545 | lldb::addr_t value, |
| 546 | bool value_is_offset) |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 547 | { |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 548 | ModuleSP module_sp = GetModule(); |
| 549 | if (module_sp) |
| 550 | { |
| 551 | size_t num_loaded_sections = 0; |
| 552 | SectionList *section_list = GetSectionList (); |
| 553 | if (section_list) |
| 554 | { |
Greg Clayton | 751caf6 | 2014-02-07 22:54:47 +0000 | [diff] [blame] | 555 | if (value_is_offset) |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 556 | { |
Greg Clayton | 751caf6 | 2014-02-07 22:54:47 +0000 | [diff] [blame] | 557 | const size_t num_sections = section_list->GetSize(); |
| 558 | size_t sect_idx = 0; |
Sylvestre Ledru | f561a01 | 2014-02-21 18:08:09 +0000 | [diff] [blame] | 559 | |
Greg Clayton | 751caf6 | 2014-02-07 22:54:47 +0000 | [diff] [blame] | 560 | for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 561 | { |
Greg Clayton | 751caf6 | 2014-02-07 22:54:47 +0000 | [diff] [blame] | 562 | // Iterate through the object file sections to find all |
| 563 | // of the sections that have SHF_ALLOC in their flag bits. |
| 564 | SectionSP section_sp (section_list->GetSectionAtIndex (sect_idx)); |
| 565 | // if (section_sp && !section_sp->IsThreadSpecific()) |
| 566 | if (section_sp && section_sp->Test(SHF_ALLOC)) |
| 567 | { |
| 568 | if (target.GetSectionLoadList().SetSectionLoadAddress (section_sp, section_sp->GetFileAddress() + value)) |
| 569 | ++num_loaded_sections; |
| 570 | } |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 571 | } |
Greg Clayton | 751caf6 | 2014-02-07 22:54:47 +0000 | [diff] [blame] | 572 | return num_loaded_sections > 0; |
| 573 | } |
| 574 | else |
| 575 | { |
| 576 | // Not sure how to slide an ELF file given the base address |
| 577 | // of the ELF file in memory |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 578 | } |
| 579 | } |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 580 | } |
Sylvestre Ledru | f561a01 | 2014-02-21 18:08:09 +0000 | [diff] [blame] | 581 | return false; // If it changed |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 584 | ByteOrder |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 585 | ObjectFileELF::GetByteOrder() const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 586 | { |
| 587 | if (m_header.e_ident[EI_DATA] == ELFDATA2MSB) |
| 588 | return eByteOrderBig; |
| 589 | if (m_header.e_ident[EI_DATA] == ELFDATA2LSB) |
| 590 | return eByteOrderLittle; |
| 591 | return eByteOrderInvalid; |
| 592 | } |
| 593 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 594 | uint32_t |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 595 | ObjectFileELF::GetAddressByteSize() const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 596 | { |
| 597 | return m_data.GetAddressByteSize(); |
| 598 | } |
| 599 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 600 | size_t |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 601 | ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 602 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 603 | return std::distance(m_section_headers.begin(), I) + 1u; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 604 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 605 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 606 | size_t |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 607 | ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const |
| 608 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 609 | return std::distance(m_section_headers.begin(), I) + 1u; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | bool |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 613 | ObjectFileELF::ParseHeader() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 614 | { |
Filipe Cabecinhas | 22b40f7 | 2013-05-16 23:29:36 +0000 | [diff] [blame] | 615 | lldb::offset_t offset = 0; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 616 | return m_header.Parse(m_data, &offset); |
| 617 | } |
| 618 | |
| 619 | bool |
Greg Clayton | 6083026 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 620 | ObjectFileELF::GetUUID(lldb_private::UUID* uuid) |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 621 | { |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 622 | // Need to parse the section list to get the UUIDs, so make sure that's been done. |
| 623 | if (!ParseSectionHeaders()) |
| 624 | return false; |
| 625 | |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 626 | if (m_uuid.IsValid()) |
| 627 | { |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 628 | // We have the full build id uuid. |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 629 | *uuid = m_uuid; |
| 630 | return true; |
| 631 | } |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 632 | else |
| 633 | { |
Michael Sartain | 9f4517a | 2013-07-03 01:52:14 +0000 | [diff] [blame] | 634 | if (!m_gnu_debuglink_crc) |
| 635 | m_gnu_debuglink_crc = calc_gnu_debuglink_crc32 (m_data.GetDataStart(), m_data.GetByteSize()); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 636 | if (m_gnu_debuglink_crc) |
| 637 | { |
| 638 | // Use 4 bytes of crc from the .gnu_debuglink section. |
| 639 | uint32_t uuidt[4] = { m_gnu_debuglink_crc, 0, 0, 0 }; |
| 640 | uuid->SetBytes (uuidt, sizeof(uuidt)); |
| 641 | return true; |
| 642 | } |
| 643 | } |
| 644 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 645 | return false; |
| 646 | } |
| 647 | |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 648 | lldb_private::FileSpecList |
| 649 | ObjectFileELF::GetDebugSymbolFilePaths() |
| 650 | { |
| 651 | FileSpecList file_spec_list; |
| 652 | |
| 653 | if (!m_gnu_debuglink_file.empty()) |
| 654 | { |
| 655 | FileSpec file_spec (m_gnu_debuglink_file.c_str(), false); |
| 656 | file_spec_list.Append (file_spec); |
| 657 | } |
| 658 | return file_spec_list; |
| 659 | } |
| 660 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 661 | uint32_t |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 662 | ObjectFileELF::GetDependentModules(FileSpecList &files) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 663 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 664 | size_t num_modules = ParseDependentModules(); |
| 665 | uint32_t num_specs = 0; |
| 666 | |
| 667 | for (unsigned i = 0; i < num_modules; ++i) |
| 668 | { |
| 669 | if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i))) |
| 670 | num_specs++; |
| 671 | } |
| 672 | |
| 673 | return num_specs; |
| 674 | } |
| 675 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 676 | Address |
Ed Maste | 5480365 | 2013-10-11 17:39:07 +0000 | [diff] [blame] | 677 | ObjectFileELF::GetImageInfoAddress(Target *target) |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 678 | { |
| 679 | if (!ParseDynamicSymbols()) |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 680 | return Address(); |
| 681 | |
| 682 | SectionList *section_list = GetSectionList(); |
| 683 | if (!section_list) |
| 684 | return Address(); |
| 685 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 686 | // Find the SHT_DYNAMIC (.dynamic) section. |
| 687 | SectionSP dynsym_section_sp (section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true)); |
| 688 | if (!dynsym_section_sp) |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 689 | return Address(); |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 690 | assert (dynsym_section_sp->GetObjectFile() == this); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 691 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 692 | user_id_t dynsym_id = dynsym_section_sp->GetID(); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 693 | const ELFSectionHeaderInfo *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 694 | if (!dynsym_hdr) |
| 695 | return Address(); |
| 696 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 697 | for (size_t i = 0; i < m_dynamic_symbols.size(); ++i) |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 698 | { |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 699 | ELFDynamic &symbol = m_dynamic_symbols[i]; |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 700 | |
Ed Maste | 5480365 | 2013-10-11 17:39:07 +0000 | [diff] [blame] | 701 | if (symbol.d_tag == DT_DEBUG) |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 702 | { |
| 703 | // Compute the offset as the number of previous entries plus the |
| 704 | // size of d_tag. |
| 705 | addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize(); |
| 706 | return Address(dynsym_section_sp, offset); |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 707 | } |
Ed Maste | 5480365 | 2013-10-11 17:39:07 +0000 | [diff] [blame] | 708 | else if (symbol.d_tag == DT_MIPS_RLD_MAP && target) |
| 709 | { |
| 710 | addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize(); |
| 711 | addr_t dyn_base = dynsym_section_sp->GetLoadBaseAddress(target); |
| 712 | if (dyn_base == LLDB_INVALID_ADDRESS) |
| 713 | return Address(); |
| 714 | Address addr; |
| 715 | Error error; |
| 716 | if (target->ReadPointerFromMemory(dyn_base + offset, false, error, addr)) |
| 717 | return addr; |
| 718 | } |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | return Address(); |
| 722 | } |
| 723 | |
Jim Ingham | 672e6f5 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 724 | lldb_private::Address |
| 725 | ObjectFileELF::GetEntryPointAddress () |
| 726 | { |
Stephen Wilson | d126c8c | 2011-03-08 04:12:15 +0000 | [diff] [blame] | 727 | if (m_entry_point_address.IsValid()) |
| 728 | return m_entry_point_address; |
| 729 | |
| 730 | if (!ParseHeader() || !IsExecutable()) |
| 731 | return m_entry_point_address; |
| 732 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 733 | SectionList *section_list = GetSectionList(); |
| 734 | addr_t offset = m_header.e_entry; |
Stephen Wilson | d126c8c | 2011-03-08 04:12:15 +0000 | [diff] [blame] | 735 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 736 | if (!section_list) |
Stephen Wilson | d126c8c | 2011-03-08 04:12:15 +0000 | [diff] [blame] | 737 | m_entry_point_address.SetOffset(offset); |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 738 | else |
| 739 | m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list); |
Stephen Wilson | d126c8c | 2011-03-08 04:12:15 +0000 | [diff] [blame] | 740 | return m_entry_point_address; |
Jim Ingham | 672e6f5 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 743 | //---------------------------------------------------------------------- |
| 744 | // ParseDependentModules |
| 745 | //---------------------------------------------------------------------- |
| 746 | size_t |
| 747 | ObjectFileELF::ParseDependentModules() |
| 748 | { |
| 749 | if (m_filespec_ap.get()) |
| 750 | return m_filespec_ap->GetSize(); |
| 751 | |
| 752 | m_filespec_ap.reset(new FileSpecList()); |
| 753 | |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 754 | if (!ParseSectionHeaders()) |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 755 | return 0; |
| 756 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 757 | SectionList *section_list = GetSectionList(); |
| 758 | if (!section_list) |
| 759 | return 0; |
| 760 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 761 | // Find the SHT_DYNAMIC section. |
| 762 | Section *dynsym = section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true).get(); |
| 763 | if (!dynsym) |
| 764 | return 0; |
| 765 | assert (dynsym->GetObjectFile() == this); |
| 766 | |
| 767 | const ELFSectionHeaderInfo *header = GetSectionHeaderByIndex (dynsym->GetID()); |
| 768 | if (!header) |
| 769 | return 0; |
| 770 | // sh_link: section header index of string table used by entries in the section. |
| 771 | Section *dynstr = section_list->FindSectionByID (header->sh_link + 1).get(); |
| 772 | if (!dynstr) |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 773 | return 0; |
| 774 | |
| 775 | DataExtractor dynsym_data; |
| 776 | DataExtractor dynstr_data; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 777 | if (ReadSectionData(dynsym, dynsym_data) && |
| 778 | ReadSectionData(dynstr, dynstr_data)) |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 779 | { |
| 780 | ELFDynamic symbol; |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 781 | const lldb::offset_t section_size = dynsym_data.GetByteSize(); |
| 782 | lldb::offset_t offset = 0; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 783 | |
| 784 | // The only type of entries we are concerned with are tagged DT_NEEDED, |
| 785 | // yielding the name of a required library. |
| 786 | while (offset < section_size) |
| 787 | { |
| 788 | if (!symbol.Parse(dynsym_data, &offset)) |
| 789 | break; |
| 790 | |
| 791 | if (symbol.d_tag != DT_NEEDED) |
| 792 | continue; |
| 793 | |
| 794 | uint32_t str_index = static_cast<uint32_t>(symbol.d_val); |
| 795 | const char *lib_name = dynstr_data.PeekCStr(str_index); |
Greg Clayton | 274060b | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 796 | m_filespec_ap->Append(FileSpec(lib_name, true)); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | |
| 800 | return m_filespec_ap->GetSize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | //---------------------------------------------------------------------- |
| 804 | // ParseProgramHeaders |
| 805 | //---------------------------------------------------------------------- |
| 806 | size_t |
| 807 | ObjectFileELF::ParseProgramHeaders() |
| 808 | { |
| 809 | // We have already parsed the program headers |
| 810 | if (!m_program_headers.empty()) |
| 811 | return m_program_headers.size(); |
| 812 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 813 | // If there are no program headers to read we are done. |
| 814 | if (m_header.e_phnum == 0) |
| 815 | return 0; |
| 816 | |
| 817 | m_program_headers.resize(m_header.e_phnum); |
| 818 | if (m_program_headers.size() != m_header.e_phnum) |
| 819 | return 0; |
| 820 | |
| 821 | const size_t ph_size = m_header.e_phnum * m_header.e_phentsize; |
Greg Clayton | 44435ed | 2012-01-12 05:25:17 +0000 | [diff] [blame] | 822 | const elf_off ph_offset = m_header.e_phoff; |
| 823 | DataExtractor data; |
| 824 | if (GetData (ph_offset, ph_size, data) != ph_size) |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 825 | return 0; |
| 826 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 827 | uint32_t idx; |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 828 | lldb::offset_t offset; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 829 | for (idx = 0, offset = 0; idx < m_header.e_phnum; ++idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 830 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 831 | if (m_program_headers[idx].Parse(data, &offset) == false) |
| 832 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 835 | if (idx < m_program_headers.size()) |
| 836 | m_program_headers.resize(idx); |
| 837 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 838 | return m_program_headers.size(); |
| 839 | } |
| 840 | |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 841 | static bool |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 842 | ParseNoteGNUBuildID(DataExtractor &data, lldb_private::UUID &uuid) |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 843 | { |
| 844 | // Try to parse the note section (ie .note.gnu.build-id|.notes|.note|...) and get the build id. |
| 845 | // BuildID documentation: https://fedoraproject.org/wiki/Releases/FeatureBuildId |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 846 | lldb::offset_t offset = 0; |
| 847 | static const uint32_t g_gnu_build_id = 3; // NT_GNU_BUILD_ID from elf.h |
| 848 | |
| 849 | while (true) |
| 850 | { |
Ed Maste | c113ff8 | 2013-12-02 17:49:13 +0000 | [diff] [blame] | 851 | ELFNote note = ELFNote(); |
| 852 | if (!note.Parse(data, &offset)) |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 853 | return false; |
| 854 | |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 855 | // 16 bytes is UUID|MD5, 20 bytes is SHA1 |
Ed Maste | c113ff8 | 2013-12-02 17:49:13 +0000 | [diff] [blame] | 856 | if (note.n_name == "GNU" && (note.n_type == g_gnu_build_id) && |
| 857 | (note.n_descsz == 16 || note.n_descsz == 20)) |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 858 | { |
Ed Maste | c113ff8 | 2013-12-02 17:49:13 +0000 | [diff] [blame] | 859 | uint8_t uuidbuf[20]; |
| 860 | if (data.GetU8 (&offset, &uuidbuf, note.n_descsz) == NULL) |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 861 | return false; |
Ed Maste | c113ff8 | 2013-12-02 17:49:13 +0000 | [diff] [blame] | 862 | uuid.SetBytes (uuidbuf, note.n_descsz); |
| 863 | return true; |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 864 | } |
Ed Maste | c113ff8 | 2013-12-02 17:49:13 +0000 | [diff] [blame] | 865 | offset += llvm::RoundUpToAlignment(note.n_descsz, 4); |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 866 | } |
| 867 | return false; |
| 868 | } |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 869 | |
| 870 | //---------------------------------------------------------------------- |
| 871 | // GetSectionHeaderInfo |
| 872 | //---------------------------------------------------------------------- |
| 873 | size_t |
| 874 | ObjectFileELF::GetSectionHeaderInfo(SectionHeaderColl §ion_headers, |
| 875 | lldb_private::DataExtractor &object_data, |
| 876 | const elf::ELFHeader &header, |
| 877 | lldb_private::UUID &uuid, |
| 878 | std::string &gnu_debuglink_file, |
| 879 | uint32_t &gnu_debuglink_crc) |
| 880 | { |
| 881 | // We have already parsed the section headers |
| 882 | if (!section_headers.empty()) |
| 883 | return section_headers.size(); |
| 884 | |
| 885 | // If there are no section headers we are done. |
| 886 | if (header.e_shnum == 0) |
| 887 | return 0; |
| 888 | |
| 889 | section_headers.resize(header.e_shnum); |
| 890 | if (section_headers.size() != header.e_shnum) |
| 891 | return 0; |
| 892 | |
| 893 | const size_t sh_size = header.e_shnum * header.e_shentsize; |
| 894 | const elf_off sh_offset = header.e_shoff; |
| 895 | DataExtractor sh_data; |
| 896 | if (sh_data.SetData (object_data, sh_offset, sh_size) != sh_size) |
| 897 | return 0; |
| 898 | |
| 899 | uint32_t idx; |
| 900 | lldb::offset_t offset; |
| 901 | for (idx = 0, offset = 0; idx < header.e_shnum; ++idx) |
| 902 | { |
| 903 | if (section_headers[idx].Parse(sh_data, &offset) == false) |
| 904 | break; |
| 905 | } |
| 906 | if (idx < section_headers.size()) |
| 907 | section_headers.resize(idx); |
| 908 | |
| 909 | const unsigned strtab_idx = header.e_shstrndx; |
| 910 | if (strtab_idx && strtab_idx < section_headers.size()) |
| 911 | { |
| 912 | const ELFSectionHeaderInfo &sheader = section_headers[strtab_idx]; |
| 913 | const size_t byte_size = sheader.sh_size; |
| 914 | const Elf64_Off offset = sheader.sh_offset; |
| 915 | lldb_private::DataExtractor shstr_data; |
| 916 | |
| 917 | if (shstr_data.SetData (object_data, offset, byte_size) == byte_size) |
| 918 | { |
| 919 | for (SectionHeaderCollIter I = section_headers.begin(); |
| 920 | I != section_headers.end(); ++I) |
| 921 | { |
| 922 | static ConstString g_sect_name_gnu_debuglink (".gnu_debuglink"); |
| 923 | const ELFSectionHeaderInfo &header = *I; |
| 924 | const uint64_t section_size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size; |
| 925 | ConstString name(shstr_data.PeekCStr(I->sh_name)); |
| 926 | |
| 927 | I->section_name = name; |
| 928 | |
| 929 | if (name == g_sect_name_gnu_debuglink) |
| 930 | { |
| 931 | DataExtractor data; |
| 932 | if (section_size && (data.SetData (object_data, header.sh_offset, section_size) == section_size)) |
| 933 | { |
| 934 | lldb::offset_t gnu_debuglink_offset = 0; |
| 935 | gnu_debuglink_file = data.GetCStr (&gnu_debuglink_offset); |
| 936 | gnu_debuglink_offset = llvm::RoundUpToAlignment (gnu_debuglink_offset, 4); |
| 937 | data.GetU32 (&gnu_debuglink_offset, &gnu_debuglink_crc, 1); |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | if (header.sh_type == SHT_NOTE && !uuid.IsValid()) |
| 942 | { |
| 943 | DataExtractor data; |
| 944 | if (section_size && (data.SetData (object_data, header.sh_offset, section_size) == section_size)) |
| 945 | { |
| 946 | ParseNoteGNUBuildID (data, uuid); |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | return section_headers.size(); |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | section_headers.clear(); |
| 956 | return 0; |
| 957 | } |
| 958 | |
Ashok Thirumurthi | 4822d92 | 2013-07-11 20:39:00 +0000 | [diff] [blame] | 959 | size_t |
| 960 | ObjectFileELF::GetProgramHeaderCount() |
| 961 | { |
| 962 | return ParseProgramHeaders(); |
| 963 | } |
| 964 | |
| 965 | const elf::ELFProgramHeader * |
| 966 | ObjectFileELF::GetProgramHeaderByIndex(lldb::user_id_t id) |
| 967 | { |
| 968 | if (!id || !ParseProgramHeaders()) |
| 969 | return NULL; |
| 970 | |
| 971 | if (--id < m_program_headers.size()) |
| 972 | return &m_program_headers[id]; |
| 973 | |
| 974 | return NULL; |
| 975 | } |
| 976 | |
| 977 | DataExtractor |
| 978 | ObjectFileELF::GetSegmentDataByIndex(lldb::user_id_t id) |
| 979 | { |
| 980 | const elf::ELFProgramHeader *segment_header = GetProgramHeaderByIndex(id); |
| 981 | if (segment_header == NULL) |
| 982 | return DataExtractor(); |
| 983 | return DataExtractor(m_data, segment_header->p_offset, segment_header->p_filesz); |
| 984 | } |
| 985 | |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 986 | //---------------------------------------------------------------------- |
| 987 | // ParseSectionHeaders |
| 988 | //---------------------------------------------------------------------- |
| 989 | size_t |
| 990 | ObjectFileELF::ParseSectionHeaders() |
| 991 | { |
| 992 | return GetSectionHeaderInfo(m_section_headers, m_data, m_header, m_uuid, m_gnu_debuglink_file, m_gnu_debuglink_crc); |
| 993 | } |
| 994 | |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 995 | const ObjectFileELF::ELFSectionHeaderInfo * |
| 996 | ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id) |
| 997 | { |
Ashok Thirumurthi | 4822d92 | 2013-07-11 20:39:00 +0000 | [diff] [blame] | 998 | if (!id || !ParseSectionHeaders()) |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 999 | return NULL; |
| 1000 | |
| 1001 | if (--id < m_section_headers.size()) |
| 1002 | return &m_section_headers[id]; |
| 1003 | |
| 1004 | return NULL; |
| 1005 | } |
| 1006 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1007 | void |
| 1008 | ObjectFileELF::CreateSections(SectionList &unified_section_list) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1009 | { |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1010 | if (!m_sections_ap.get() && ParseSectionHeaders()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1011 | { |
| 1012 | m_sections_ap.reset(new SectionList()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1013 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1014 | for (SectionHeaderCollIter I = m_section_headers.begin(); |
| 1015 | I != m_section_headers.end(); ++I) |
| 1016 | { |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1017 | const ELFSectionHeaderInfo &header = *I; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1018 | |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1019 | ConstString& name = I->section_name; |
Greg Clayton | 47037bc | 2012-03-27 02:40:46 +0000 | [diff] [blame] | 1020 | const uint64_t file_size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size; |
| 1021 | const uint64_t vm_size = header.sh_flags & SHF_ALLOC ? header.sh_size : 0; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1022 | |
Greg Clayton | 4ceb998 | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 1023 | static ConstString g_sect_name_text (".text"); |
| 1024 | static ConstString g_sect_name_data (".data"); |
| 1025 | static ConstString g_sect_name_bss (".bss"); |
Greg Clayton | 741f3f9 | 2012-03-27 21:10:07 +0000 | [diff] [blame] | 1026 | static ConstString g_sect_name_tdata (".tdata"); |
| 1027 | static ConstString g_sect_name_tbss (".tbss"); |
Greg Clayton | 4ceb998 | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 1028 | static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev"); |
| 1029 | static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges"); |
| 1030 | static ConstString g_sect_name_dwarf_debug_frame (".debug_frame"); |
| 1031 | static ConstString g_sect_name_dwarf_debug_info (".debug_info"); |
| 1032 | static ConstString g_sect_name_dwarf_debug_line (".debug_line"); |
| 1033 | static ConstString g_sect_name_dwarf_debug_loc (".debug_loc"); |
| 1034 | static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo"); |
| 1035 | static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames"); |
| 1036 | static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes"); |
| 1037 | static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges"); |
| 1038 | static ConstString g_sect_name_dwarf_debug_str (".debug_str"); |
| 1039 | static ConstString g_sect_name_eh_frame (".eh_frame"); |
| 1040 | |
| 1041 | SectionType sect_type = eSectionTypeOther; |
| 1042 | |
Greg Clayton | 741f3f9 | 2012-03-27 21:10:07 +0000 | [diff] [blame] | 1043 | bool is_thread_specific = false; |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1044 | |
Greg Clayton | 4ceb998 | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 1045 | if (name == g_sect_name_text) sect_type = eSectionTypeCode; |
| 1046 | else if (name == g_sect_name_data) sect_type = eSectionTypeData; |
| 1047 | else if (name == g_sect_name_bss) sect_type = eSectionTypeZeroFill; |
Greg Clayton | 741f3f9 | 2012-03-27 21:10:07 +0000 | [diff] [blame] | 1048 | else if (name == g_sect_name_tdata) |
| 1049 | { |
| 1050 | sect_type = eSectionTypeData; |
| 1051 | is_thread_specific = true; |
| 1052 | } |
| 1053 | else if (name == g_sect_name_tbss) |
| 1054 | { |
| 1055 | sect_type = eSectionTypeZeroFill; |
| 1056 | is_thread_specific = true; |
| 1057 | } |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1058 | // .debug_abbrev – Abbreviations used in the .debug_info section |
| 1059 | // .debug_aranges – Lookup table for mapping addresses to compilation units |
| 1060 | // .debug_frame – Call frame information |
| 1061 | // .debug_info – The core DWARF information section |
| 1062 | // .debug_line – Line number information |
| 1063 | // .debug_loc – Location lists used in DW_AT_location attributes |
| 1064 | // .debug_macinfo – Macro information |
| 1065 | // .debug_pubnames – Lookup table for mapping object and function names to compilation units |
| 1066 | // .debug_pubtypes – Lookup table for mapping type names to compilation units |
| 1067 | // .debug_ranges – Address ranges used in DW_AT_ranges attributes |
| 1068 | // .debug_str – String table used in .debug_info |
Michael Sartain | 3cf443d | 2013-07-17 00:26:30 +0000 | [diff] [blame] | 1069 | // MISSING? .gnu_debugdata - "mini debuginfo / MiniDebugInfo" section, http://sourceware.org/gdb/onlinedocs/gdb/MiniDebugInfo.html |
| 1070 | // MISSING? .debug-index - http://src.chromium.org/viewvc/chrome/trunk/src/build/gdb-add-index?pathrev=144644 |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1071 | // MISSING? .debug_types - Type descriptions from DWARF 4? See http://gcc.gnu.org/wiki/DwarfSeparateTypeInfo |
Greg Clayton | 4ceb998 | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 1072 | else if (name == g_sect_name_dwarf_debug_abbrev) sect_type = eSectionTypeDWARFDebugAbbrev; |
| 1073 | else if (name == g_sect_name_dwarf_debug_aranges) sect_type = eSectionTypeDWARFDebugAranges; |
| 1074 | else if (name == g_sect_name_dwarf_debug_frame) sect_type = eSectionTypeDWARFDebugFrame; |
| 1075 | else if (name == g_sect_name_dwarf_debug_info) sect_type = eSectionTypeDWARFDebugInfo; |
| 1076 | else if (name == g_sect_name_dwarf_debug_line) sect_type = eSectionTypeDWARFDebugLine; |
| 1077 | else if (name == g_sect_name_dwarf_debug_loc) sect_type = eSectionTypeDWARFDebugLoc; |
| 1078 | else if (name == g_sect_name_dwarf_debug_macinfo) sect_type = eSectionTypeDWARFDebugMacInfo; |
| 1079 | else if (name == g_sect_name_dwarf_debug_pubnames) sect_type = eSectionTypeDWARFDebugPubNames; |
| 1080 | else if (name == g_sect_name_dwarf_debug_pubtypes) sect_type = eSectionTypeDWARFDebugPubTypes; |
| 1081 | else if (name == g_sect_name_dwarf_debug_ranges) sect_type = eSectionTypeDWARFDebugRanges; |
| 1082 | else if (name == g_sect_name_dwarf_debug_str) sect_type = eSectionTypeDWARFDebugStr; |
| 1083 | else if (name == g_sect_name_eh_frame) sect_type = eSectionTypeEHFrame; |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1084 | |
| 1085 | switch (header.sh_type) |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 1086 | { |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1087 | case SHT_SYMTAB: |
| 1088 | assert (sect_type == eSectionTypeOther); |
| 1089 | sect_type = eSectionTypeELFSymbolTable; |
| 1090 | break; |
| 1091 | case SHT_DYNSYM: |
| 1092 | assert (sect_type == eSectionTypeOther); |
| 1093 | sect_type = eSectionTypeELFDynamicSymbols; |
| 1094 | break; |
| 1095 | case SHT_RELA: |
| 1096 | case SHT_REL: |
| 1097 | assert (sect_type == eSectionTypeOther); |
| 1098 | sect_type = eSectionTypeELFRelocationEntries; |
| 1099 | break; |
| 1100 | case SHT_DYNAMIC: |
| 1101 | assert (sect_type == eSectionTypeOther); |
| 1102 | sect_type = eSectionTypeELFDynamicLinkInfo; |
| 1103 | break; |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 1104 | } |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1105 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1106 | SectionSP section_sp (new Section(GetModule(), // Module to which this section belongs. |
| 1107 | this, // ObjectFile to which this section belongs and should read section data from. |
| 1108 | SectionIndex(I), // Section ID. |
| 1109 | name, // Section name. |
| 1110 | sect_type, // Section type. |
| 1111 | header.sh_addr, // VM address. |
| 1112 | vm_size, // VM size in bytes of this section. |
| 1113 | header.sh_offset, // Offset of this section in the file. |
| 1114 | file_size, // Size of the section as found in the file. |
| 1115 | header.sh_flags)); // Flags for this section. |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1116 | |
Greg Clayton | 741f3f9 | 2012-03-27 21:10:07 +0000 | [diff] [blame] | 1117 | if (is_thread_specific) |
| 1118 | section_sp->SetIsThreadSpecific (is_thread_specific); |
| 1119 | m_sections_ap->AddSection(section_sp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1120 | } |
| 1121 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1122 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1123 | if (m_sections_ap.get()) |
| 1124 | { |
| 1125 | if (GetType() == eTypeDebugInfo) |
| 1126 | { |
| 1127 | static const SectionType g_sections[] = |
| 1128 | { |
| 1129 | eSectionTypeDWARFDebugAranges, |
| 1130 | eSectionTypeDWARFDebugInfo, |
| 1131 | eSectionTypeDWARFDebugAbbrev, |
| 1132 | eSectionTypeDWARFDebugFrame, |
| 1133 | eSectionTypeDWARFDebugLine, |
| 1134 | eSectionTypeDWARFDebugStr, |
| 1135 | eSectionTypeDWARFDebugLoc, |
| 1136 | eSectionTypeDWARFDebugMacInfo, |
| 1137 | eSectionTypeDWARFDebugPubNames, |
| 1138 | eSectionTypeDWARFDebugPubTypes, |
| 1139 | eSectionTypeDWARFDebugRanges, |
| 1140 | eSectionTypeELFSymbolTable, |
| 1141 | }; |
| 1142 | SectionList *elf_section_list = m_sections_ap.get(); |
| 1143 | for (size_t idx = 0; idx < sizeof(g_sections) / sizeof(g_sections[0]); ++idx) |
| 1144 | { |
| 1145 | SectionType section_type = g_sections[idx]; |
| 1146 | SectionSP section_sp (elf_section_list->FindSectionByType (section_type, true)); |
| 1147 | if (section_sp) |
| 1148 | { |
| 1149 | SectionSP module_section_sp (unified_section_list.FindSectionByType (section_type, true)); |
| 1150 | if (module_section_sp) |
| 1151 | unified_section_list.ReplaceSection (module_section_sp->GetID(), section_sp); |
| 1152 | else |
| 1153 | unified_section_list.AddSection (section_sp); |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | else |
| 1158 | { |
| 1159 | unified_section_list = *m_sections_ap; |
| 1160 | } |
| 1161 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1164 | // private |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1165 | unsigned |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1166 | ObjectFileELF::ParseSymbols (Symtab *symtab, |
| 1167 | user_id_t start_id, |
| 1168 | SectionList *section_list, |
| 1169 | const size_t num_symbols, |
| 1170 | const DataExtractor &symtab_data, |
| 1171 | const DataExtractor &strtab_data) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1172 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1173 | ELFSymbol symbol; |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1174 | lldb::offset_t offset = 0; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1175 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1176 | static ConstString text_section_name(".text"); |
| 1177 | static ConstString init_section_name(".init"); |
| 1178 | static ConstString fini_section_name(".fini"); |
| 1179 | static ConstString ctors_section_name(".ctors"); |
| 1180 | static ConstString dtors_section_name(".dtors"); |
| 1181 | |
| 1182 | static ConstString data_section_name(".data"); |
| 1183 | static ConstString rodata_section_name(".rodata"); |
| 1184 | static ConstString rodata1_section_name(".rodata1"); |
| 1185 | static ConstString data2_section_name(".data1"); |
| 1186 | static ConstString bss_section_name(".bss"); |
| 1187 | |
Greg Clayton | 9594f4c | 2013-04-13 23:17:23 +0000 | [diff] [blame] | 1188 | //StreamFile strm(stdout, false); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1189 | unsigned i; |
| 1190 | for (i = 0; i < num_symbols; ++i) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1191 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1192 | if (symbol.Parse(symtab_data, &offset) == false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1193 | break; |
Greg Clayton | 9594f4c | 2013-04-13 23:17:23 +0000 | [diff] [blame] | 1194 | |
| 1195 | const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); |
| 1196 | |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 1197 | // No need to add non-section symbols that have no names |
| 1198 | if (symbol.getType() != STT_SECTION && |
| 1199 | (symbol_name == NULL || symbol_name[0] == '\0')) |
Greg Clayton | 9594f4c | 2013-04-13 23:17:23 +0000 | [diff] [blame] | 1200 | continue; |
| 1201 | |
| 1202 | //symbol.Dump (&strm, i, &strtab_data, section_list); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1203 | |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1204 | SectionSP symbol_section_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1205 | SymbolType symbol_type = eSymbolTypeInvalid; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1206 | Elf64_Half symbol_idx = symbol.st_shndx; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1207 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1208 | switch (symbol_idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1209 | { |
| 1210 | case SHN_ABS: |
| 1211 | symbol_type = eSymbolTypeAbsolute; |
| 1212 | break; |
| 1213 | case SHN_UNDEF: |
| 1214 | symbol_type = eSymbolTypeUndefined; |
| 1215 | break; |
| 1216 | default: |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1217 | symbol_section_sp = section_list->GetSectionAtIndex(symbol_idx); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1218 | break; |
| 1219 | } |
| 1220 | |
Matt Kopec | 92dd5cf | 2013-02-12 18:30:30 +0000 | [diff] [blame] | 1221 | // If a symbol is undefined do not process it further even if it has a STT type |
| 1222 | if (symbol_type != eSymbolTypeUndefined) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1223 | { |
Matt Kopec | 92dd5cf | 2013-02-12 18:30:30 +0000 | [diff] [blame] | 1224 | switch (symbol.getType()) |
| 1225 | { |
| 1226 | default: |
| 1227 | case STT_NOTYPE: |
| 1228 | // The symbol's type is not specified. |
| 1229 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1230 | |
Matt Kopec | 92dd5cf | 2013-02-12 18:30:30 +0000 | [diff] [blame] | 1231 | case STT_OBJECT: |
| 1232 | // The symbol is associated with a data object, such as a variable, |
| 1233 | // an array, etc. |
| 1234 | symbol_type = eSymbolTypeData; |
| 1235 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1236 | |
Matt Kopec | 92dd5cf | 2013-02-12 18:30:30 +0000 | [diff] [blame] | 1237 | case STT_FUNC: |
| 1238 | // The symbol is associated with a function or other executable code. |
| 1239 | symbol_type = eSymbolTypeCode; |
| 1240 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1241 | |
Matt Kopec | 92dd5cf | 2013-02-12 18:30:30 +0000 | [diff] [blame] | 1242 | case STT_SECTION: |
| 1243 | // The symbol is associated with a section. Symbol table entries of |
| 1244 | // this type exist primarily for relocation and normally have |
| 1245 | // STB_LOCAL binding. |
| 1246 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1247 | |
Matt Kopec | 92dd5cf | 2013-02-12 18:30:30 +0000 | [diff] [blame] | 1248 | case STT_FILE: |
| 1249 | // Conventionally, the symbol's name gives the name of the source |
| 1250 | // file associated with the object file. A file symbol has STB_LOCAL |
| 1251 | // binding, its section index is SHN_ABS, and it precedes the other |
| 1252 | // STB_LOCAL symbols for the file, if it is present. |
Greg Clayton | 9594f4c | 2013-04-13 23:17:23 +0000 | [diff] [blame] | 1253 | symbol_type = eSymbolTypeSourceFile; |
Matt Kopec | 92dd5cf | 2013-02-12 18:30:30 +0000 | [diff] [blame] | 1254 | break; |
Matt Kopec | 00049b8 | 2013-02-27 20:13:38 +0000 | [diff] [blame] | 1255 | |
| 1256 | case STT_GNU_IFUNC: |
| 1257 | // The symbol is associated with an indirect function. The actual |
| 1258 | // function will be resolved if it is referenced. |
| 1259 | symbol_type = eSymbolTypeResolver; |
| 1260 | break; |
Matt Kopec | 92dd5cf | 2013-02-12 18:30:30 +0000 | [diff] [blame] | 1261 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | if (symbol_type == eSymbolTypeInvalid) |
| 1265 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1266 | if (symbol_section_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1267 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1268 | const ConstString §_name = symbol_section_sp->GetName(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1269 | if (sect_name == text_section_name || |
| 1270 | sect_name == init_section_name || |
| 1271 | sect_name == fini_section_name || |
| 1272 | sect_name == ctors_section_name || |
| 1273 | sect_name == dtors_section_name) |
| 1274 | { |
| 1275 | symbol_type = eSymbolTypeCode; |
| 1276 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1277 | else if (sect_name == data_section_name || |
| 1278 | sect_name == data2_section_name || |
| 1279 | sect_name == rodata_section_name || |
| 1280 | sect_name == rodata1_section_name || |
| 1281 | sect_name == bss_section_name) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1282 | { |
| 1283 | symbol_type = eSymbolTypeData; |
| 1284 | } |
| 1285 | } |
| 1286 | } |
| 1287 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1288 | // If the symbol section we've found has no data (SHT_NOBITS), then check the module section |
| 1289 | // list. This can happen if we're parsing the debug file and it has no .text section, for example. |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1290 | if (symbol_section_sp && (symbol_section_sp->GetFileSize() == 0)) |
| 1291 | { |
| 1292 | ModuleSP module_sp(GetModule()); |
| 1293 | if (module_sp) |
| 1294 | { |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1295 | SectionList *module_section_list = module_sp->GetSectionList(); |
| 1296 | if (module_section_list && module_section_list != section_list) |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1297 | { |
| 1298 | const ConstString §_name = symbol_section_sp->GetName(); |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1299 | lldb::SectionSP section_sp (module_section_list->FindSectionByName (sect_name)); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1300 | if (section_sp && section_sp->GetFileSize()) |
| 1301 | { |
| 1302 | symbol_section_sp = section_sp; |
| 1303 | } |
| 1304 | } |
| 1305 | } |
| 1306 | } |
| 1307 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1308 | uint64_t symbol_value = symbol.st_value; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 1309 | if (symbol_section_sp && CalculateType() != ObjectFile::Type::eTypeObjectFile) |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1310 | symbol_value -= symbol_section_sp->GetFileAddress(); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1311 | bool is_global = symbol.getBinding() == STB_GLOBAL; |
| 1312 | uint32_t flags = symbol.st_other << 8 | symbol.st_info; |
Greg Clayton | 47037bc | 2012-03-27 02:40:46 +0000 | [diff] [blame] | 1313 | bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1314 | Symbol dc_symbol( |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1315 | i + start_id, // ID is the original symbol table index. |
| 1316 | symbol_name, // Symbol name. |
Greg Clayton | 47037bc | 2012-03-27 02:40:46 +0000 | [diff] [blame] | 1317 | is_mangled, // Is the symbol name mangled? |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1318 | symbol_type, // Type of this symbol |
| 1319 | is_global, // Is this globally visible? |
| 1320 | false, // Is this symbol debug info? |
| 1321 | false, // Is this symbol a trampoline? |
| 1322 | false, // Is this symbol artificial? |
| 1323 | symbol_section_sp, // Section in which this symbol is defined or null. |
| 1324 | symbol_value, // Offset in section or symbol value. |
| 1325 | symbol.st_size, // Size in bytes of this symbol. |
Greg Clayton | 9594f4c | 2013-04-13 23:17:23 +0000 | [diff] [blame] | 1326 | true, // Size is valid |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1327 | flags); // Symbol flags. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1328 | symtab->AddSymbol(dc_symbol); |
| 1329 | } |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1330 | |
| 1331 | return i; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1334 | unsigned |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1335 | ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, user_id_t start_id, lldb_private::Section *symtab) |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1336 | { |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1337 | if (symtab->GetObjectFile() != this) |
| 1338 | { |
| 1339 | // If the symbol table section is owned by a different object file, have it do the |
| 1340 | // parsing. |
| 1341 | ObjectFileELF *obj_file_elf = static_cast<ObjectFileELF *>(symtab->GetObjectFile()); |
| 1342 | return obj_file_elf->ParseSymbolTable (symbol_table, start_id, symtab); |
| 1343 | } |
| 1344 | |
| 1345 | // Get section list for this object file. |
| 1346 | SectionList *section_list = m_sections_ap.get(); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1347 | if (!section_list) |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1348 | return 0; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1349 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1350 | user_id_t symtab_id = symtab->GetID(); |
| 1351 | const ELFSectionHeaderInfo *symtab_hdr = GetSectionHeaderByIndex(symtab_id); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1352 | assert(symtab_hdr->sh_type == SHT_SYMTAB || |
| 1353 | symtab_hdr->sh_type == SHT_DYNSYM); |
| 1354 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1355 | // sh_link: section header index of associated string table. |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1356 | // Section ID's are ones based. |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1357 | user_id_t strtab_id = symtab_hdr->sh_link + 1; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1358 | Section *strtab = section_list->FindSectionByID(strtab_id).get(); |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1359 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1360 | if (symtab && strtab) |
| 1361 | { |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1362 | assert (symtab->GetObjectFile() == this); |
| 1363 | assert (strtab->GetObjectFile() == this); |
| 1364 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1365 | DataExtractor symtab_data; |
| 1366 | DataExtractor strtab_data; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 1367 | if (ReadSectionData(symtab, symtab_data) && |
| 1368 | ReadSectionData(strtab, strtab_data)) |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1369 | { |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1370 | size_t num_symbols = symtab_data.GetByteSize() / symtab_hdr->sh_entsize; |
| 1371 | |
Arnaud A. de Grandmaison | 62e5f4d | 2014-03-22 20:23:26 +0000 | [diff] [blame^] | 1372 | return ParseSymbols(symbol_table, start_id, section_list, |
| 1373 | num_symbols, symtab_data, strtab_data); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1374 | } |
| 1375 | } |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1376 | |
Arnaud A. de Grandmaison | 62e5f4d | 2014-03-22 20:23:26 +0000 | [diff] [blame^] | 1377 | return 0; |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | size_t |
| 1381 | ObjectFileELF::ParseDynamicSymbols() |
| 1382 | { |
| 1383 | if (m_dynamic_symbols.size()) |
| 1384 | return m_dynamic_symbols.size(); |
| 1385 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1386 | SectionList *section_list = GetSectionList(); |
| 1387 | if (!section_list) |
Bill Wendling | ed24dcc | 2012-04-03 07:50:11 +0000 | [diff] [blame] | 1388 | return 0; |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1389 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1390 | // Find the SHT_DYNAMIC section. |
| 1391 | Section *dynsym = section_list->FindSectionByType (eSectionTypeELFDynamicLinkInfo, true).get(); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1392 | if (!dynsym) |
Bill Wendling | ed24dcc | 2012-04-03 07:50:11 +0000 | [diff] [blame] | 1393 | return 0; |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1394 | assert (dynsym->GetObjectFile() == this); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1395 | |
| 1396 | ELFDynamic symbol; |
| 1397 | DataExtractor dynsym_data; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 1398 | if (ReadSectionData(dynsym, dynsym_data)) |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1399 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1400 | const lldb::offset_t section_size = dynsym_data.GetByteSize(); |
| 1401 | lldb::offset_t cursor = 0; |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1402 | |
| 1403 | while (cursor < section_size) |
| 1404 | { |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1405 | if (!symbol.Parse(dynsym_data, &cursor)) |
| 1406 | break; |
| 1407 | |
| 1408 | m_dynamic_symbols.push_back(symbol); |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | return m_dynamic_symbols.size(); |
| 1413 | } |
| 1414 | |
| 1415 | const ELFDynamic * |
| 1416 | ObjectFileELF::FindDynamicSymbol(unsigned tag) |
| 1417 | { |
| 1418 | if (!ParseDynamicSymbols()) |
| 1419 | return NULL; |
| 1420 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1421 | DynamicSymbolCollIter I = m_dynamic_symbols.begin(); |
| 1422 | DynamicSymbolCollIter E = m_dynamic_symbols.end(); |
| 1423 | for ( ; I != E; ++I) |
| 1424 | { |
| 1425 | ELFDynamic *symbol = &*I; |
| 1426 | |
| 1427 | if (symbol->d_tag == tag) |
| 1428 | return symbol; |
| 1429 | } |
| 1430 | |
| 1431 | return NULL; |
| 1432 | } |
| 1433 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1434 | unsigned |
| 1435 | ObjectFileELF::PLTRelocationType() |
| 1436 | { |
Michael Sartain | f789954 | 2013-08-22 21:25:53 +0000 | [diff] [blame] | 1437 | // DT_PLTREL |
| 1438 | // This member specifies the type of relocation entry to which the |
| 1439 | // procedure linkage table refers. The d_val member holds DT_REL or |
| 1440 | // DT_RELA, as appropriate. All relocations in a procedure linkage table |
| 1441 | // must use the same relocation. |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1442 | const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL); |
| 1443 | |
| 1444 | if (symbol) |
| 1445 | return symbol->d_val; |
| 1446 | |
| 1447 | return 0; |
| 1448 | } |
| 1449 | |
| 1450 | static unsigned |
| 1451 | ParsePLTRelocations(Symtab *symbol_table, |
| 1452 | user_id_t start_id, |
| 1453 | unsigned rel_type, |
| 1454 | const ELFHeader *hdr, |
| 1455 | const ELFSectionHeader *rel_hdr, |
| 1456 | const ELFSectionHeader *plt_hdr, |
| 1457 | const ELFSectionHeader *sym_hdr, |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1458 | const lldb::SectionSP &plt_section_sp, |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1459 | DataExtractor &rel_data, |
| 1460 | DataExtractor &symtab_data, |
| 1461 | DataExtractor &strtab_data) |
| 1462 | { |
| 1463 | ELFRelocation rel(rel_type); |
| 1464 | ELFSymbol symbol; |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1465 | lldb::offset_t offset = 0; |
Michael Sartain | f789954 | 2013-08-22 21:25:53 +0000 | [diff] [blame] | 1466 | // Clang 3.3 sets entsize to 4 for 32-bit binaries, but the plt entries are 16 bytes. |
| 1467 | // So round the entsize up by the alignment if addralign is set. |
| 1468 | const elf_xword plt_entsize = plt_hdr->sh_addralign ? |
| 1469 | llvm::RoundUpToAlignment (plt_hdr->sh_entsize, plt_hdr->sh_addralign) : plt_hdr->sh_entsize; |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1470 | const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1471 | |
| 1472 | typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel); |
| 1473 | reloc_info_fn reloc_type; |
| 1474 | reloc_info_fn reloc_symbol; |
| 1475 | |
Greg Clayton | d091afe | 2012-11-12 22:53:16 +0000 | [diff] [blame] | 1476 | if (hdr->Is32Bit()) |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1477 | { |
| 1478 | reloc_type = ELFRelocation::RelocType32; |
| 1479 | reloc_symbol = ELFRelocation::RelocSymbol32; |
| 1480 | } |
| 1481 | else |
| 1482 | { |
| 1483 | reloc_type = ELFRelocation::RelocType64; |
| 1484 | reloc_symbol = ELFRelocation::RelocSymbol64; |
| 1485 | } |
| 1486 | |
| 1487 | unsigned slot_type = hdr->GetRelocationJumpSlotType(); |
| 1488 | unsigned i; |
| 1489 | for (i = 0; i < num_relocations; ++i) |
| 1490 | { |
| 1491 | if (rel.Parse(rel_data, &offset) == false) |
| 1492 | break; |
| 1493 | |
| 1494 | if (reloc_type(rel) != slot_type) |
| 1495 | continue; |
| 1496 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1497 | lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize; |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1498 | uint64_t plt_index = (i + 1) * plt_entsize; |
| 1499 | |
| 1500 | if (!symbol.Parse(symtab_data, &symbol_offset)) |
| 1501 | break; |
| 1502 | |
| 1503 | const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); |
Greg Clayton | 47037bc | 2012-03-27 02:40:46 +0000 | [diff] [blame] | 1504 | bool is_mangled = symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false; |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1505 | |
| 1506 | Symbol jump_symbol( |
| 1507 | i + start_id, // Symbol table index |
| 1508 | symbol_name, // symbol name. |
Greg Clayton | 47037bc | 2012-03-27 02:40:46 +0000 | [diff] [blame] | 1509 | is_mangled, // is the symbol name mangled? |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1510 | eSymbolTypeTrampoline, // Type of this symbol |
| 1511 | false, // Is this globally visible? |
| 1512 | false, // Is this symbol debug info? |
| 1513 | true, // Is this symbol a trampoline? |
| 1514 | true, // Is this symbol artificial? |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1515 | plt_section_sp, // Section in which this symbol is defined or null. |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1516 | plt_index, // Offset in section or symbol value. |
| 1517 | plt_entsize, // Size in bytes of this symbol. |
Greg Clayton | 9594f4c | 2013-04-13 23:17:23 +0000 | [diff] [blame] | 1518 | true, // Size is valid |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1519 | 0); // Symbol flags. |
| 1520 | |
| 1521 | symbol_table->AddSymbol(jump_symbol); |
| 1522 | } |
| 1523 | |
| 1524 | return i; |
| 1525 | } |
| 1526 | |
| 1527 | unsigned |
| 1528 | ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table, |
| 1529 | user_id_t start_id, |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1530 | const ELFSectionHeaderInfo *rel_hdr, |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1531 | user_id_t rel_id) |
| 1532 | { |
| 1533 | assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL); |
| 1534 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1535 | // The link field points to the associated symbol table. The info field |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1536 | // points to the section holding the plt. |
| 1537 | user_id_t symtab_id = rel_hdr->sh_link; |
| 1538 | user_id_t plt_id = rel_hdr->sh_info; |
| 1539 | |
| 1540 | if (!symtab_id || !plt_id) |
| 1541 | return 0; |
| 1542 | |
| 1543 | // Section ID's are ones based; |
| 1544 | symtab_id++; |
| 1545 | plt_id++; |
| 1546 | |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1547 | const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1548 | if (!plt_hdr) |
| 1549 | return 0; |
| 1550 | |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1551 | const ELFSectionHeaderInfo *sym_hdr = GetSectionHeaderByIndex(symtab_id); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1552 | if (!sym_hdr) |
| 1553 | return 0; |
| 1554 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1555 | SectionList *section_list = m_sections_ap.get(); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1556 | if (!section_list) |
| 1557 | return 0; |
| 1558 | |
| 1559 | Section *rel_section = section_list->FindSectionByID(rel_id).get(); |
| 1560 | if (!rel_section) |
| 1561 | return 0; |
| 1562 | |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1563 | SectionSP plt_section_sp (section_list->FindSectionByID(plt_id)); |
| 1564 | if (!plt_section_sp) |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1565 | return 0; |
| 1566 | |
| 1567 | Section *symtab = section_list->FindSectionByID(symtab_id).get(); |
| 1568 | if (!symtab) |
| 1569 | return 0; |
| 1570 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1571 | // sh_link points to associated string table. |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1572 | Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get(); |
| 1573 | if (!strtab) |
| 1574 | return 0; |
| 1575 | |
| 1576 | DataExtractor rel_data; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 1577 | if (!ReadSectionData(rel_section, rel_data)) |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1578 | return 0; |
| 1579 | |
| 1580 | DataExtractor symtab_data; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 1581 | if (!ReadSectionData(symtab, symtab_data)) |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1582 | return 0; |
| 1583 | |
| 1584 | DataExtractor strtab_data; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 1585 | if (!ReadSectionData(strtab, strtab_data)) |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1586 | return 0; |
| 1587 | |
| 1588 | unsigned rel_type = PLTRelocationType(); |
| 1589 | if (!rel_type) |
| 1590 | return 0; |
| 1591 | |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1592 | return ParsePLTRelocations (symbol_table, |
| 1593 | start_id, |
| 1594 | rel_type, |
| 1595 | &m_header, |
| 1596 | rel_hdr, |
| 1597 | plt_hdr, |
| 1598 | sym_hdr, |
| 1599 | plt_section_sp, |
| 1600 | rel_data, |
| 1601 | symtab_data, |
| 1602 | strtab_data); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1603 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1604 | |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 1605 | unsigned |
| 1606 | ObjectFileELF::RelocateSection(Symtab* symtab, const ELFHeader *hdr, const ELFSectionHeader *rel_hdr, |
| 1607 | const ELFSectionHeader *symtab_hdr, const ELFSectionHeader *debug_hdr, |
| 1608 | DataExtractor &rel_data, DataExtractor &symtab_data, |
| 1609 | DataExtractor &debug_data, Section* rel_section) |
| 1610 | { |
| 1611 | ELFRelocation rel(rel_hdr->sh_type); |
| 1612 | lldb::addr_t offset = 0; |
| 1613 | const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; |
| 1614 | typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel); |
| 1615 | reloc_info_fn reloc_type; |
| 1616 | reloc_info_fn reloc_symbol; |
| 1617 | |
| 1618 | if (hdr->Is32Bit()) |
| 1619 | { |
| 1620 | reloc_type = ELFRelocation::RelocType32; |
| 1621 | reloc_symbol = ELFRelocation::RelocSymbol32; |
| 1622 | } |
| 1623 | else |
| 1624 | { |
| 1625 | reloc_type = ELFRelocation::RelocType64; |
| 1626 | reloc_symbol = ELFRelocation::RelocSymbol64; |
| 1627 | } |
| 1628 | |
| 1629 | for (unsigned i = 0; i < num_relocations; ++i) |
| 1630 | { |
| 1631 | if (rel.Parse(rel_data, &offset) == false) |
| 1632 | break; |
| 1633 | |
| 1634 | Symbol* symbol = NULL; |
| 1635 | |
| 1636 | if (hdr->Is32Bit()) |
| 1637 | { |
| 1638 | switch (reloc_type(rel)) { |
| 1639 | case R_386_32: |
| 1640 | case R_386_PC32: |
| 1641 | default: |
| 1642 | assert(false && "unexpected relocation type"); |
| 1643 | } |
| 1644 | } else { |
| 1645 | switch (reloc_type(rel)) { |
| 1646 | case R_X86_64_64: |
| 1647 | { |
| 1648 | symbol = symtab->FindSymbolByID(reloc_symbol(rel)); |
| 1649 | if (symbol) |
| 1650 | { |
| 1651 | addr_t value = symbol->GetAddress().GetFileAddress(); |
| 1652 | DataBufferSP& data_buffer_sp = debug_data.GetSharedDataBuffer(); |
| 1653 | uint64_t* dst = reinterpret_cast<uint64_t*>(data_buffer_sp->GetBytes() + rel_section->GetFileOffset() + ELFRelocation::RelocOffset64(rel)); |
| 1654 | *dst = value + ELFRelocation::RelocAddend64(rel); |
| 1655 | } |
| 1656 | break; |
| 1657 | } |
| 1658 | case R_X86_64_32: |
| 1659 | case R_X86_64_32S: |
| 1660 | { |
| 1661 | symbol = symtab->FindSymbolByID(reloc_symbol(rel)); |
| 1662 | if (symbol) |
| 1663 | { |
| 1664 | addr_t value = symbol->GetAddress().GetFileAddress(); |
| 1665 | value += ELFRelocation::RelocAddend32(rel); |
| 1666 | assert((reloc_type(rel) == R_X86_64_32 && (value <= UINT32_MAX)) || |
| 1667 | (reloc_type(rel) == R_X86_64_32S && |
| 1668 | ((int64_t)value <= INT32_MAX && (int64_t)value >= INT32_MIN))); |
| 1669 | uint32_t truncated_addr = (value & 0xFFFFFFFF); |
| 1670 | DataBufferSP& data_buffer_sp = debug_data.GetSharedDataBuffer(); |
| 1671 | uint32_t* dst = reinterpret_cast<uint32_t*>(data_buffer_sp->GetBytes() + rel_section->GetFileOffset() + ELFRelocation::RelocOffset32(rel)); |
| 1672 | *dst = truncated_addr; |
| 1673 | } |
| 1674 | break; |
| 1675 | } |
| 1676 | case R_X86_64_PC32: |
| 1677 | default: |
| 1678 | assert(false && "unexpected relocation type"); |
| 1679 | } |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | return 0; |
| 1684 | } |
| 1685 | |
| 1686 | unsigned |
| 1687 | ObjectFileELF::RelocateDebugSections(const ELFSectionHeader *rel_hdr, user_id_t rel_id) |
| 1688 | { |
| 1689 | assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL); |
| 1690 | |
| 1691 | // Parse in the section list if needed. |
| 1692 | SectionList *section_list = GetSectionList(); |
| 1693 | if (!section_list) |
| 1694 | return 0; |
| 1695 | |
| 1696 | // Section ID's are ones based. |
| 1697 | user_id_t symtab_id = rel_hdr->sh_link + 1; |
| 1698 | user_id_t debug_id = rel_hdr->sh_info + 1; |
| 1699 | |
| 1700 | const ELFSectionHeader *symtab_hdr = GetSectionHeaderByIndex(symtab_id); |
| 1701 | if (!symtab_hdr) |
| 1702 | return 0; |
| 1703 | |
| 1704 | const ELFSectionHeader *debug_hdr = GetSectionHeaderByIndex(debug_id); |
| 1705 | if (!debug_hdr) |
| 1706 | return 0; |
| 1707 | |
| 1708 | Section *rel = section_list->FindSectionByID(rel_id).get(); |
| 1709 | if (!rel) |
| 1710 | return 0; |
| 1711 | |
| 1712 | Section *symtab = section_list->FindSectionByID(symtab_id).get(); |
| 1713 | if (!symtab) |
| 1714 | return 0; |
| 1715 | |
| 1716 | Section *debug = section_list->FindSectionByID(debug_id).get(); |
| 1717 | if (!debug) |
| 1718 | return 0; |
| 1719 | |
| 1720 | DataExtractor rel_data; |
| 1721 | DataExtractor symtab_data; |
| 1722 | DataExtractor debug_data; |
| 1723 | |
| 1724 | if (ReadSectionData(rel, rel_data) && |
| 1725 | ReadSectionData(symtab, symtab_data) && |
| 1726 | ReadSectionData(debug, debug_data)) |
| 1727 | { |
| 1728 | RelocateSection(m_symtab_ap.get(), &m_header, rel_hdr, symtab_hdr, debug_hdr, |
| 1729 | rel_data, symtab_data, debug_data, debug); |
| 1730 | } |
| 1731 | |
| 1732 | return 0; |
| 1733 | } |
| 1734 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1735 | Symtab * |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1736 | ObjectFileELF::GetSymtab() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1737 | { |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1738 | ModuleSP module_sp(GetModule()); |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1739 | if (!module_sp) |
| 1740 | return NULL; |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1741 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1742 | // We always want to use the main object file so we (hopefully) only have one cached copy |
| 1743 | // of our symtab, dynamic sections, etc. |
| 1744 | ObjectFile *module_obj_file = module_sp->GetObjectFile(); |
| 1745 | if (module_obj_file && module_obj_file != this) |
| 1746 | return module_obj_file->GetSymtab(); |
| 1747 | |
| 1748 | if (m_symtab_ap.get() == NULL) |
| 1749 | { |
| 1750 | SectionList *section_list = GetSectionList(); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1751 | if (!section_list) |
| 1752 | return NULL; |
| 1753 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1754 | uint64_t symbol_id = 0; |
| 1755 | lldb_private::Mutex::Locker locker(module_sp->GetMutex()); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1756 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1757 | m_symtab_ap.reset(new Symtab(this)); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1758 | |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1759 | // Sharable objects and dynamic executables usually have 2 distinct symbol |
| 1760 | // tables, one named ".symtab", and the other ".dynsym". The dynsym is a smaller |
| 1761 | // version of the symtab that only contains global symbols. The information found |
| 1762 | // in the dynsym is therefore also found in the symtab, while the reverse is not |
| 1763 | // necessarily true. |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1764 | Section *symtab = section_list->FindSectionByType (eSectionTypeELFSymbolTable, true).get(); |
| 1765 | if (!symtab) |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1766 | { |
| 1767 | // The symtab section is non-allocable and can be stripped, so if it doesn't exist |
| 1768 | // then use the dynsym section which should always be there. |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1769 | symtab = section_list->FindSectionByType (eSectionTypeELFDynamicSymbols, true).get(); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1770 | } |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1771 | if (symtab) |
| 1772 | symbol_id += ParseSymbolTable (m_symtab_ap.get(), symbol_id, symtab); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1773 | |
Michael Sartain | f789954 | 2013-08-22 21:25:53 +0000 | [diff] [blame] | 1774 | // DT_JMPREL |
| 1775 | // If present, this entry's d_ptr member holds the address of relocation |
| 1776 | // entries associated solely with the procedure linkage table. Separating |
| 1777 | // these relocation entries lets the dynamic linker ignore them during |
| 1778 | // process initialization, if lazy binding is enabled. If this entry is |
| 1779 | // present, the related entries of types DT_PLTRELSZ and DT_PLTREL must |
| 1780 | // also be present. |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1781 | const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL); |
| 1782 | if (symbol) |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1783 | { |
Michael Sartain | f789954 | 2013-08-22 21:25:53 +0000 | [diff] [blame] | 1784 | // Synthesize trampoline symbols to help navigate the PLT. |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1785 | addr_t addr = symbol->d_ptr; |
| 1786 | Section *reloc_section = section_list->FindSectionContainingFileAddress(addr).get(); |
| 1787 | if (reloc_section) |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1788 | { |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1789 | user_id_t reloc_id = reloc_section->GetID(); |
| 1790 | const ELFSectionHeaderInfo *reloc_header = GetSectionHeaderByIndex(reloc_id); |
| 1791 | assert(reloc_header); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1792 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1793 | ParseTrampolineSymbols (m_symtab_ap.get(), symbol_id, reloc_header, reloc_id); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1794 | } |
| 1795 | } |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1796 | } |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 1797 | |
| 1798 | for (SectionHeaderCollIter I = m_section_headers.begin(); |
| 1799 | I != m_section_headers.end(); ++I) |
| 1800 | { |
| 1801 | if (I->sh_type == SHT_RELA || I->sh_type == SHT_REL) |
| 1802 | { |
| 1803 | if (CalculateType() == eTypeObjectFile) |
| 1804 | { |
| 1805 | const char *section_name = I->section_name.AsCString(""); |
| 1806 | if (strstr(section_name, ".rela.debug") || |
| 1807 | strstr(section_name, ".rel.debug")) |
| 1808 | { |
| 1809 | const ELFSectionHeader &reloc_header = *I; |
| 1810 | user_id_t reloc_id = SectionIndex(I); |
| 1811 | RelocateDebugSections(&reloc_header, reloc_id); |
| 1812 | } |
| 1813 | } |
| 1814 | } |
| 1815 | } |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1816 | return m_symtab_ap.get(); |
| 1817 | } |
| 1818 | |
Ashok Thirumurthi | 35729bb | 2013-09-24 15:34:13 +0000 | [diff] [blame] | 1819 | Symbol * |
| 1820 | ObjectFileELF::ResolveSymbolForAddress(const Address& so_addr, bool verify_unique) |
| 1821 | { |
| 1822 | if (!m_symtab_ap.get()) |
| 1823 | return nullptr; // GetSymtab() should be called first. |
| 1824 | |
| 1825 | const SectionList *section_list = GetSectionList(); |
| 1826 | if (!section_list) |
| 1827 | return nullptr; |
| 1828 | |
| 1829 | if (DWARFCallFrameInfo *eh_frame = GetUnwindTable().GetEHFrameInfo()) |
| 1830 | { |
| 1831 | AddressRange range; |
| 1832 | if (eh_frame->GetAddressRange (so_addr, range)) |
| 1833 | { |
| 1834 | const addr_t file_addr = range.GetBaseAddress().GetFileAddress(); |
| 1835 | Symbol * symbol = verify_unique ? m_symtab_ap->FindSymbolContainingFileAddress(file_addr) : nullptr; |
| 1836 | if (symbol) |
| 1837 | return symbol; |
| 1838 | |
| 1839 | // Note that a (stripped) symbol won't be found by GetSymtab()... |
| 1840 | lldb::SectionSP eh_sym_section_sp = section_list->FindSectionContainingFileAddress(file_addr); |
| 1841 | if (eh_sym_section_sp.get()) |
| 1842 | { |
| 1843 | addr_t section_base = eh_sym_section_sp->GetFileAddress(); |
| 1844 | addr_t offset = file_addr - section_base; |
| 1845 | uint64_t symbol_id = m_symtab_ap->GetNumSymbols(); |
| 1846 | |
| 1847 | Symbol eh_symbol( |
| 1848 | symbol_id, // Symbol table index. |
| 1849 | "???", // Symbol name. |
| 1850 | false, // Is the symbol name mangled? |
| 1851 | eSymbolTypeCode, // Type of this symbol. |
| 1852 | true, // Is this globally visible? |
| 1853 | false, // Is this symbol debug info? |
| 1854 | false, // Is this symbol a trampoline? |
| 1855 | true, // Is this symbol artificial? |
| 1856 | eh_sym_section_sp, // Section in which this symbol is defined or null. |
| 1857 | offset, // Offset in section or symbol value. |
| 1858 | range.GetByteSize(), // Size in bytes of this symbol. |
| 1859 | true, // Size is valid. |
| 1860 | 0); // Symbol flags. |
| 1861 | if (symbol_id == m_symtab_ap->AddSymbol(eh_symbol)) |
| 1862 | return m_symtab_ap->SymbolAtIndex(symbol_id); |
| 1863 | } |
| 1864 | } |
| 1865 | } |
| 1866 | return nullptr; |
| 1867 | } |
| 1868 | |
| 1869 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1870 | bool |
| 1871 | ObjectFileELF::IsStripped () |
| 1872 | { |
| 1873 | // TODO: determine this for ELF |
| 1874 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1877 | //===----------------------------------------------------------------------===// |
| 1878 | // Dump |
| 1879 | // |
| 1880 | // Dump the specifics of the runtime file container (such as any headers |
| 1881 | // segments, sections, etc). |
| 1882 | //---------------------------------------------------------------------- |
| 1883 | void |
| 1884 | ObjectFileELF::Dump(Stream *s) |
| 1885 | { |
| 1886 | DumpELFHeader(s, m_header); |
| 1887 | s->EOL(); |
| 1888 | DumpELFProgramHeaders(s); |
| 1889 | s->EOL(); |
| 1890 | DumpELFSectionHeaders(s); |
| 1891 | s->EOL(); |
| 1892 | SectionList *section_list = GetSectionList(); |
| 1893 | if (section_list) |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 1894 | section_list->Dump(s, NULL, true, UINT32_MAX); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1895 | Symtab *symtab = GetSymtab(); |
| 1896 | if (symtab) |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 1897 | symtab->Dump(s, NULL, eSortOrderNone); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1898 | s->EOL(); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1899 | DumpDependentModules(s); |
| 1900 | s->EOL(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | //---------------------------------------------------------------------- |
| 1904 | // DumpELFHeader |
| 1905 | // |
| 1906 | // Dump the ELF header to the specified output stream |
| 1907 | //---------------------------------------------------------------------- |
| 1908 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1909 | ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1910 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1911 | s->PutCString("ELF Header\n"); |
| 1912 | s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]); |
| 1913 | s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n", |
| 1914 | header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]); |
| 1915 | s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n", |
| 1916 | header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]); |
| 1917 | s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n", |
| 1918 | header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1919 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1920 | s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]); |
| 1921 | s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1922 | DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]); |
| 1923 | s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]); |
| 1924 | s->Printf ("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]); |
| 1925 | |
| 1926 | s->Printf("e_type = 0x%4.4x ", header.e_type); |
| 1927 | DumpELFHeader_e_type(s, header.e_type); |
| 1928 | s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine); |
| 1929 | s->Printf("e_version = 0x%8.8x\n", header.e_version); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1930 | s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry); |
| 1931 | s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff); |
| 1932 | s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1933 | s->Printf("e_flags = 0x%8.8x\n", header.e_flags); |
| 1934 | s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize); |
| 1935 | s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize); |
| 1936 | s->Printf("e_phnum = 0x%4.4x\n", header.e_phnum); |
| 1937 | s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize); |
| 1938 | s->Printf("e_shnum = 0x%4.4x\n", header.e_shnum); |
| 1939 | s->Printf("e_shstrndx = 0x%4.4x\n", header.e_shstrndx); |
| 1940 | } |
| 1941 | |
| 1942 | //---------------------------------------------------------------------- |
| 1943 | // DumpELFHeader_e_type |
| 1944 | // |
| 1945 | // Dump an token value for the ELF header member e_type |
| 1946 | //---------------------------------------------------------------------- |
| 1947 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1948 | ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1949 | { |
| 1950 | switch (e_type) |
| 1951 | { |
| 1952 | case ET_NONE: *s << "ET_NONE"; break; |
| 1953 | case ET_REL: *s << "ET_REL"; break; |
| 1954 | case ET_EXEC: *s << "ET_EXEC"; break; |
| 1955 | case ET_DYN: *s << "ET_DYN"; break; |
| 1956 | case ET_CORE: *s << "ET_CORE"; break; |
| 1957 | default: |
| 1958 | break; |
| 1959 | } |
| 1960 | } |
| 1961 | |
| 1962 | //---------------------------------------------------------------------- |
| 1963 | // DumpELFHeader_e_ident_EI_DATA |
| 1964 | // |
| 1965 | // Dump an token value for the ELF header member e_ident[EI_DATA] |
| 1966 | //---------------------------------------------------------------------- |
| 1967 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1968 | ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1969 | { |
| 1970 | switch (ei_data) |
| 1971 | { |
| 1972 | case ELFDATANONE: *s << "ELFDATANONE"; break; |
| 1973 | case ELFDATA2LSB: *s << "ELFDATA2LSB - Little Endian"; break; |
| 1974 | case ELFDATA2MSB: *s << "ELFDATA2MSB - Big Endian"; break; |
| 1975 | default: |
| 1976 | break; |
| 1977 | } |
| 1978 | } |
| 1979 | |
| 1980 | |
| 1981 | //---------------------------------------------------------------------- |
| 1982 | // DumpELFProgramHeader |
| 1983 | // |
| 1984 | // Dump a single ELF program header to the specified output stream |
| 1985 | //---------------------------------------------------------------------- |
| 1986 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1987 | ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1988 | { |
| 1989 | DumpELFProgramHeader_p_type(s, ph.p_type); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1990 | s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset, ph.p_vaddr, ph.p_paddr); |
| 1991 | s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8x (", ph.p_filesz, ph.p_memsz, ph.p_flags); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1992 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1993 | DumpELFProgramHeader_p_flags(s, ph.p_flags); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1994 | s->Printf(") %8.8" PRIx64, ph.p_align); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | //---------------------------------------------------------------------- |
| 1998 | // DumpELFProgramHeader_p_type |
| 1999 | // |
| 2000 | // Dump an token value for the ELF program header member p_type which |
| 2001 | // describes the type of the program header |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 2002 | // ---------------------------------------------------------------------- |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2003 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 2004 | ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2005 | { |
Filipe Cabecinhas | 477d86d | 2013-05-23 23:01:14 +0000 | [diff] [blame] | 2006 | const int kStrWidth = 15; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2007 | switch (p_type) |
| 2008 | { |
Filipe Cabecinhas | 477d86d | 2013-05-23 23:01:14 +0000 | [diff] [blame] | 2009 | CASE_AND_STREAM(s, PT_NULL , kStrWidth); |
| 2010 | CASE_AND_STREAM(s, PT_LOAD , kStrWidth); |
| 2011 | CASE_AND_STREAM(s, PT_DYNAMIC , kStrWidth); |
| 2012 | CASE_AND_STREAM(s, PT_INTERP , kStrWidth); |
| 2013 | CASE_AND_STREAM(s, PT_NOTE , kStrWidth); |
| 2014 | CASE_AND_STREAM(s, PT_SHLIB , kStrWidth); |
| 2015 | CASE_AND_STREAM(s, PT_PHDR , kStrWidth); |
| 2016 | CASE_AND_STREAM(s, PT_TLS , kStrWidth); |
| 2017 | CASE_AND_STREAM(s, PT_GNU_EH_FRAME, kStrWidth); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2018 | default: |
| 2019 | s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, ""); |
| 2020 | break; |
| 2021 | } |
| 2022 | } |
| 2023 | |
| 2024 | |
| 2025 | //---------------------------------------------------------------------- |
| 2026 | // DumpELFProgramHeader_p_flags |
| 2027 | // |
| 2028 | // Dump an token value for the ELF program header member p_flags |
| 2029 | //---------------------------------------------------------------------- |
| 2030 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 2031 | ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2032 | { |
| 2033 | *s << ((p_flags & PF_X) ? "PF_X" : " ") |
| 2034 | << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ') |
| 2035 | << ((p_flags & PF_W) ? "PF_W" : " ") |
| 2036 | << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ') |
| 2037 | << ((p_flags & PF_R) ? "PF_R" : " "); |
| 2038 | } |
| 2039 | |
| 2040 | //---------------------------------------------------------------------- |
| 2041 | // DumpELFProgramHeaders |
| 2042 | // |
| 2043 | // Dump all of the ELF program header to the specified output stream |
| 2044 | //---------------------------------------------------------------------- |
| 2045 | void |
| 2046 | ObjectFileELF::DumpELFProgramHeaders(Stream *s) |
| 2047 | { |
| 2048 | if (ParseProgramHeaders()) |
| 2049 | { |
| 2050 | s->PutCString("Program Headers\n"); |
Filipe Cabecinhas | 477d86d | 2013-05-23 23:01:14 +0000 | [diff] [blame] | 2051 | s->PutCString("IDX p_type p_offset p_vaddr p_paddr " |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 2052 | "p_filesz p_memsz p_flags p_align\n"); |
Filipe Cabecinhas | 477d86d | 2013-05-23 23:01:14 +0000 | [diff] [blame] | 2053 | s->PutCString("==== --------------- -------- -------- -------- " |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 2054 | "-------- -------- ------------------------- --------\n"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2055 | |
| 2056 | uint32_t idx = 0; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 2057 | for (ProgramHeaderCollConstIter I = m_program_headers.begin(); |
| 2058 | I != m_program_headers.end(); ++I, ++idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2059 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 2060 | s->Printf("[%2u] ", idx); |
| 2061 | ObjectFileELF::DumpELFProgramHeader(s, *I); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2062 | s->EOL(); |
| 2063 | } |
| 2064 | } |
| 2065 | } |
| 2066 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2067 | //---------------------------------------------------------------------- |
| 2068 | // DumpELFSectionHeader |
| 2069 | // |
| 2070 | // Dump a single ELF section header to the specified output stream |
| 2071 | //---------------------------------------------------------------------- |
| 2072 | void |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 2073 | ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeaderInfo &sh) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2074 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 2075 | s->Printf("%8.8x ", sh.sh_name); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2076 | DumpELFSectionHeader_sh_type(s, sh.sh_type); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2077 | s->Printf(" %8.8" PRIx64 " (", sh.sh_flags); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2078 | DumpELFSectionHeader_sh_flags(s, sh.sh_flags); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2079 | s->Printf(") %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addr, sh.sh_offset, sh.sh_size); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 2080 | s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 2081 | s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | //---------------------------------------------------------------------- |
| 2085 | // DumpELFSectionHeader_sh_type |
| 2086 | // |
| 2087 | // Dump an token value for the ELF section header member sh_type which |
| 2088 | // describes the type of the section |
| 2089 | //---------------------------------------------------------------------- |
| 2090 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 2091 | ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2092 | { |
| 2093 | const int kStrWidth = 12; |
| 2094 | switch (sh_type) |
| 2095 | { |
| 2096 | CASE_AND_STREAM(s, SHT_NULL , kStrWidth); |
| 2097 | CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth); |
| 2098 | CASE_AND_STREAM(s, SHT_SYMTAB , kStrWidth); |
| 2099 | CASE_AND_STREAM(s, SHT_STRTAB , kStrWidth); |
| 2100 | CASE_AND_STREAM(s, SHT_RELA , kStrWidth); |
| 2101 | CASE_AND_STREAM(s, SHT_HASH , kStrWidth); |
| 2102 | CASE_AND_STREAM(s, SHT_DYNAMIC , kStrWidth); |
| 2103 | CASE_AND_STREAM(s, SHT_NOTE , kStrWidth); |
| 2104 | CASE_AND_STREAM(s, SHT_NOBITS , kStrWidth); |
| 2105 | CASE_AND_STREAM(s, SHT_REL , kStrWidth); |
| 2106 | CASE_AND_STREAM(s, SHT_SHLIB , kStrWidth); |
| 2107 | CASE_AND_STREAM(s, SHT_DYNSYM , kStrWidth); |
| 2108 | CASE_AND_STREAM(s, SHT_LOPROC , kStrWidth); |
| 2109 | CASE_AND_STREAM(s, SHT_HIPROC , kStrWidth); |
| 2110 | CASE_AND_STREAM(s, SHT_LOUSER , kStrWidth); |
| 2111 | CASE_AND_STREAM(s, SHT_HIUSER , kStrWidth); |
| 2112 | default: |
| 2113 | s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, ""); |
| 2114 | break; |
| 2115 | } |
| 2116 | } |
| 2117 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2118 | //---------------------------------------------------------------------- |
| 2119 | // DumpELFSectionHeader_sh_flags |
| 2120 | // |
| 2121 | // Dump an token value for the ELF section header member sh_flags |
| 2122 | //---------------------------------------------------------------------- |
| 2123 | void |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 2124 | ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_xword sh_flags) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2125 | { |
| 2126 | *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ") |
| 2127 | << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ') |
| 2128 | << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ") |
| 2129 | << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ') |
| 2130 | << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " "); |
| 2131 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 2132 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2133 | //---------------------------------------------------------------------- |
| 2134 | // DumpELFSectionHeaders |
| 2135 | // |
| 2136 | // Dump all of the ELF section header to the specified output stream |
| 2137 | //---------------------------------------------------------------------- |
| 2138 | void |
| 2139 | ObjectFileELF::DumpELFSectionHeaders(Stream *s) |
| 2140 | { |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 2141 | if (!ParseSectionHeaders()) |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 2142 | return; |
| 2143 | |
| 2144 | s->PutCString("Section Headers\n"); |
| 2145 | s->PutCString("IDX name type flags " |
| 2146 | "addr offset size link info addralgn " |
| 2147 | "entsize Name\n"); |
| 2148 | s->PutCString("==== -------- ------------ -------------------------------- " |
| 2149 | "-------- -------- -------- -------- -------- -------- " |
| 2150 | "-------- ====================\n"); |
| 2151 | |
| 2152 | uint32_t idx = 0; |
| 2153 | for (SectionHeaderCollConstIter I = m_section_headers.begin(); |
| 2154 | I != m_section_headers.end(); ++I, ++idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2155 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 2156 | s->Printf("[%2u] ", idx); |
| 2157 | ObjectFileELF::DumpELFSectionHeader(s, *I); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 2158 | const char* section_name = I->section_name.AsCString(""); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 2159 | if (section_name) |
| 2160 | *s << ' ' << section_name << "\n"; |
| 2161 | } |
| 2162 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2163 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 2164 | void |
| 2165 | ObjectFileELF::DumpDependentModules(lldb_private::Stream *s) |
| 2166 | { |
| 2167 | size_t num_modules = ParseDependentModules(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2168 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 2169 | if (num_modules > 0) |
| 2170 | { |
| 2171 | s->PutCString("Dependent Modules:\n"); |
| 2172 | for (unsigned i = 0; i < num_modules; ++i) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2173 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 2174 | const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i); |
| 2175 | s->Printf(" %s\n", spec.GetFilename().GetCString()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2176 | } |
| 2177 | } |
| 2178 | } |
| 2179 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2180 | bool |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 2181 | ObjectFileELF::GetArchitecture (ArchSpec &arch) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2182 | { |
Stephen Wilson | 3f4200fd | 2011-02-24 19:16:15 +0000 | [diff] [blame] | 2183 | if (!ParseHeader()) |
| 2184 | return false; |
| 2185 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 2186 | arch.SetArchitecture (eArchTypeELF, m_header.e_machine, LLDB_INVALID_CPUTYPE); |
Greg Clayton | 64195a2 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 2187 | arch.GetTriple().setOSName (Host::GetOSString().GetCString()); |
| 2188 | arch.GetTriple().setVendorName(Host::GetVendorString().GetCString()); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 2189 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
Greg Clayton | 9e00b6a65 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 2192 | ObjectFile::Type |
| 2193 | ObjectFileELF::CalculateType() |
| 2194 | { |
| 2195 | switch (m_header.e_type) |
| 2196 | { |
| 2197 | case llvm::ELF::ET_NONE: |
| 2198 | // 0 - No file type |
| 2199 | return eTypeUnknown; |
| 2200 | |
| 2201 | case llvm::ELF::ET_REL: |
| 2202 | // 1 - Relocatable file |
| 2203 | return eTypeObjectFile; |
| 2204 | |
| 2205 | case llvm::ELF::ET_EXEC: |
| 2206 | // 2 - Executable file |
| 2207 | return eTypeExecutable; |
| 2208 | |
| 2209 | case llvm::ELF::ET_DYN: |
| 2210 | // 3 - Shared object file |
| 2211 | return eTypeSharedLibrary; |
| 2212 | |
| 2213 | case ET_CORE: |
| 2214 | // 4 - Core file |
| 2215 | return eTypeCoreFile; |
| 2216 | |
| 2217 | default: |
| 2218 | break; |
| 2219 | } |
| 2220 | return eTypeUnknown; |
| 2221 | } |
| 2222 | |
| 2223 | ObjectFile::Strata |
| 2224 | ObjectFileELF::CalculateStrata() |
| 2225 | { |
| 2226 | switch (m_header.e_type) |
| 2227 | { |
| 2228 | case llvm::ELF::ET_NONE: |
| 2229 | // 0 - No file type |
| 2230 | return eStrataUnknown; |
| 2231 | |
| 2232 | case llvm::ELF::ET_REL: |
| 2233 | // 1 - Relocatable file |
| 2234 | return eStrataUnknown; |
| 2235 | |
| 2236 | case llvm::ELF::ET_EXEC: |
| 2237 | // 2 - Executable file |
| 2238 | // TODO: is there any way to detect that an executable is a kernel |
| 2239 | // related executable by inspecting the program headers, section |
| 2240 | // headers, symbols, or any other flag bits??? |
| 2241 | return eStrataUser; |
| 2242 | |
| 2243 | case llvm::ELF::ET_DYN: |
| 2244 | // 3 - Shared object file |
| 2245 | // TODO: is there any way to detect that an shared library is a kernel |
| 2246 | // related executable by inspecting the program headers, section |
| 2247 | // headers, symbols, or any other flag bits??? |
| 2248 | return eStrataUnknown; |
| 2249 | |
| 2250 | case ET_CORE: |
| 2251 | // 4 - Core file |
| 2252 | // TODO: is there any way to detect that an core file is a kernel |
| 2253 | // related executable by inspecting the program headers, section |
| 2254 | // headers, symbols, or any other flag bits??? |
| 2255 | return eStrataUnknown; |
| 2256 | |
| 2257 | default: |
| 2258 | break; |
| 2259 | } |
| 2260 | return eStrataUnknown; |
| 2261 | } |
| 2262 | |