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