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" |
Jim Ingham | 672e6f5 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 19 | #include "lldb/Core/Module.h" |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 20 | #include "lldb/Core/ModuleSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Core/PluginManager.h" |
| 22 | #include "lldb/Core/Section.h" |
| 23 | #include "lldb/Core/Stream.h" |
Jim Ingham | 672e6f5 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 24 | #include "lldb/Symbol/SymbolContext.h" |
Greg Clayton | 64195a2 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 25 | #include "lldb/Host/Host.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/PointerUnion.h" |
| 28 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 29 | #define CASE_AND_STREAM(s, def, width) \ |
| 30 | case def: s->Printf("%-*s", width, #def); break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | using namespace lldb; |
| 33 | using namespace lldb_private; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 34 | using namespace elf; |
| 35 | using namespace llvm::ELF; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 37 | namespace { |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | /// @class ELFRelocation |
| 40 | /// @brief Generic wrapper for ELFRel and ELFRela. |
| 41 | /// |
| 42 | /// This helper class allows us to parse both ELFRel and ELFRela relocation |
| 43 | /// entries in a generic manner. |
| 44 | class ELFRelocation |
| 45 | { |
| 46 | public: |
| 47 | |
| 48 | /// Constructs an ELFRelocation entry with a personality as given by @p |
| 49 | /// type. |
| 50 | /// |
| 51 | /// @param type Either DT_REL or DT_RELA. Any other value is invalid. |
| 52 | ELFRelocation(unsigned type); |
| 53 | |
| 54 | ~ELFRelocation(); |
| 55 | |
| 56 | bool |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 57 | Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 58 | |
| 59 | static unsigned |
| 60 | RelocType32(const ELFRelocation &rel); |
| 61 | |
| 62 | static unsigned |
| 63 | RelocType64(const ELFRelocation &rel); |
| 64 | |
| 65 | static unsigned |
| 66 | RelocSymbol32(const ELFRelocation &rel); |
| 67 | |
| 68 | static unsigned |
| 69 | RelocSymbol64(const ELFRelocation &rel); |
| 70 | |
| 71 | private: |
| 72 | typedef llvm::PointerUnion<ELFRel*, ELFRela*> RelocUnion; |
| 73 | |
| 74 | RelocUnion reloc; |
| 75 | }; |
| 76 | |
| 77 | ELFRelocation::ELFRelocation(unsigned type) |
| 78 | { |
| 79 | if (type == DT_REL) |
| 80 | reloc = new ELFRel(); |
| 81 | else if (type == DT_RELA) |
| 82 | reloc = new ELFRela(); |
| 83 | else { |
| 84 | assert(false && "unexpected relocation type"); |
| 85 | reloc = static_cast<ELFRel*>(NULL); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | ELFRelocation::~ELFRelocation() |
| 90 | { |
| 91 | if (reloc.is<ELFRel*>()) |
| 92 | delete reloc.get<ELFRel*>(); |
| 93 | else |
| 94 | delete reloc.get<ELFRela*>(); |
| 95 | } |
| 96 | |
| 97 | bool |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 98 | ELFRelocation::Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset) |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 99 | { |
| 100 | if (reloc.is<ELFRel*>()) |
| 101 | return reloc.get<ELFRel*>()->Parse(data, offset); |
| 102 | else |
| 103 | return reloc.get<ELFRela*>()->Parse(data, offset); |
| 104 | } |
| 105 | |
| 106 | unsigned |
| 107 | ELFRelocation::RelocType32(const ELFRelocation &rel) |
| 108 | { |
| 109 | if (rel.reloc.is<ELFRel*>()) |
| 110 | return ELFRel::RelocType32(*rel.reloc.get<ELFRel*>()); |
| 111 | else |
| 112 | return ELFRela::RelocType32(*rel.reloc.get<ELFRela*>()); |
| 113 | } |
| 114 | |
| 115 | unsigned |
| 116 | ELFRelocation::RelocType64(const ELFRelocation &rel) |
| 117 | { |
| 118 | if (rel.reloc.is<ELFRel*>()) |
| 119 | return ELFRel::RelocType64(*rel.reloc.get<ELFRel*>()); |
| 120 | else |
| 121 | return ELFRela::RelocType64(*rel.reloc.get<ELFRela*>()); |
| 122 | } |
| 123 | |
| 124 | unsigned |
| 125 | ELFRelocation::RelocSymbol32(const ELFRelocation &rel) |
| 126 | { |
| 127 | if (rel.reloc.is<ELFRel*>()) |
| 128 | return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel*>()); |
| 129 | else |
| 130 | return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela*>()); |
| 131 | } |
| 132 | |
| 133 | unsigned |
| 134 | ELFRelocation::RelocSymbol64(const ELFRelocation &rel) |
| 135 | { |
| 136 | if (rel.reloc.is<ELFRel*>()) |
| 137 | return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel*>()); |
| 138 | else |
| 139 | return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela*>()); |
| 140 | } |
| 141 | |
| 142 | } // end anonymous namespace |
| 143 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 144 | //------------------------------------------------------------------ |
| 145 | // Static methods. |
| 146 | //------------------------------------------------------------------ |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 147 | void |
| 148 | ObjectFileELF::Initialize() |
| 149 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 150 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 151 | GetPluginDescriptionStatic(), |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 152 | CreateInstance, |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 153 | CreateMemoryInstance, |
| 154 | GetModuleSpecifications); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void |
| 158 | ObjectFileELF::Terminate() |
| 159 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 160 | PluginManager::UnregisterPlugin(CreateInstance); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 163 | lldb_private::ConstString |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 164 | ObjectFileELF::GetPluginNameStatic() |
| 165 | { |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 166 | static ConstString g_name("elf"); |
| 167 | return g_name; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | const char * |
| 171 | ObjectFileELF::GetPluginDescriptionStatic() |
| 172 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 173 | return "ELF object file reader."; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 176 | ObjectFile * |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 177 | ObjectFileELF::CreateInstance (const lldb::ModuleSP &module_sp, |
| 178 | DataBufferSP &data_sp, |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 179 | lldb::offset_t data_offset, |
| 180 | const lldb_private::FileSpec* file, |
| 181 | lldb::offset_t file_offset, |
| 182 | lldb::offset_t length) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 183 | { |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 184 | if (!data_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 185 | { |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 186 | data_sp = file->MemoryMapFileContents(file_offset, length); |
| 187 | data_offset = 0; |
| 188 | } |
| 189 | |
| 190 | if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset)) |
| 191 | { |
| 192 | const uint8_t *magic = data_sp->GetBytes() + data_offset; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 193 | if (ELFHeader::MagicBytesMatch(magic)) |
| 194 | { |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 195 | // 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] | 196 | if (data_sp->GetByteSize() < length) { |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 197 | data_sp = file->MemoryMapFileContents(file_offset, length); |
Greg Clayton | 64ff6c7 | 2013-02-07 21:49:54 +0000 | [diff] [blame] | 198 | data_offset = 0; |
| 199 | magic = data_sp->GetBytes(); |
Andrew Kaylor | 213f672 | 2013-02-07 21:30:54 +0000 | [diff] [blame] | 200 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 201 | unsigned address_size = ELFHeader::AddressSizeInBytes(magic); |
| 202 | if (address_size == 4 || address_size == 8) |
| 203 | { |
Greg Clayton | 7b0992d | 2013-04-18 22:45:39 +0000 | [diff] [blame] | 204 | 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] | 205 | ArchSpec spec; |
| 206 | if (objfile_ap->GetArchitecture(spec) && |
| 207 | objfile_ap->SetModulesArchitecture(spec)) |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 208 | return objfile_ap.release(); |
| 209 | } |
| 210 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 211 | } |
| 212 | return NULL; |
| 213 | } |
| 214 | |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 215 | |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 216 | ObjectFile* |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 217 | ObjectFileELF::CreateMemoryInstance (const lldb::ModuleSP &module_sp, |
| 218 | DataBufferSP& data_sp, |
| 219 | const lldb::ProcessSP &process_sp, |
| 220 | lldb::addr_t header_addr) |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 221 | { |
| 222 | return NULL; |
| 223 | } |
| 224 | |
Michael Sartain | 9f0013d | 2013-05-17 00:20:21 +0000 | [diff] [blame] | 225 | bool |
| 226 | ObjectFileELF::MagicBytesMatch (DataBufferSP& data_sp, |
| 227 | lldb::addr_t data_offset, |
| 228 | lldb::addr_t data_length) |
| 229 | { |
| 230 | if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset)) |
| 231 | { |
| 232 | const uint8_t *magic = data_sp->GetBytes() + data_offset; |
| 233 | return ELFHeader::MagicBytesMatch(magic); |
| 234 | } |
| 235 | return false; |
| 236 | } |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 237 | |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 238 | size_t |
| 239 | ObjectFileELF::GetModuleSpecifications (const lldb_private::FileSpec& file, |
| 240 | lldb::DataBufferSP& data_sp, |
| 241 | lldb::offset_t data_offset, |
| 242 | lldb::offset_t file_offset, |
| 243 | lldb::offset_t length, |
| 244 | lldb_private::ModuleSpecList &specs) |
| 245 | { |
Michael Sartain | adeab50 | 2013-05-17 02:00:55 +0000 | [diff] [blame] | 246 | // FIXME: mikesart@valvesoftware.com |
| 247 | // Implementing this function has broken several tests. Specifically this one: |
| 248 | // Python dotest.py --executable <path-to-lldb> -p TestCallStdStringFunction.py |
Michael Sartain | 9f0013d | 2013-05-17 00:20:21 +0000 | [diff] [blame] | 249 | const size_t initial_count = specs.GetSize(); |
Michael Sartain | adeab50 | 2013-05-17 02:00:55 +0000 | [diff] [blame] | 250 | #if 0 |
Michael Sartain | 9f0013d | 2013-05-17 00:20:21 +0000 | [diff] [blame] | 251 | if (ObjectFileELF::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) |
| 252 | { |
| 253 | DataExtractor data; |
| 254 | data.SetData(data_sp); |
| 255 | elf::ELFHeader header; |
| 256 | if (header.Parse(data, &data_offset)) |
| 257 | { |
| 258 | if (data_sp) |
| 259 | { |
| 260 | ModuleSpec spec; |
| 261 | spec.GetFileSpec() = file; |
| 262 | spec.GetArchitecture().SetArchitecture(eArchTypeELF, |
| 263 | header.e_machine, |
| 264 | LLDB_INVALID_CPUTYPE); |
| 265 | if (spec.GetArchitecture().IsValid()) |
| 266 | { |
| 267 | // ObjectFileMachO adds the UUID here also, but that isn't in the elf header |
| 268 | // so we'd have to read the entire file in and calculate the md5sum. |
| 269 | // That'd be bad for this routine... |
| 270 | specs.Append(spec); |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | } |
Michael Sartain | adeab50 | 2013-05-17 02:00:55 +0000 | [diff] [blame] | 275 | #endif |
Michael Sartain | 9f0013d | 2013-05-17 00:20:21 +0000 | [diff] [blame] | 276 | return specs.GetSize() - initial_count; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 279 | //------------------------------------------------------------------ |
| 280 | // PluginInterface protocol |
| 281 | //------------------------------------------------------------------ |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 282 | lldb_private::ConstString |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 283 | ObjectFileELF::GetPluginName() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 284 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 285 | return GetPluginNameStatic(); |
| 286 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 287 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 288 | uint32_t |
| 289 | ObjectFileELF::GetPluginVersion() |
| 290 | { |
| 291 | return m_plugin_version; |
| 292 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 293 | //------------------------------------------------------------------ |
| 294 | // ObjectFile protocol |
| 295 | //------------------------------------------------------------------ |
| 296 | |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 297 | ObjectFileELF::ObjectFileELF (const lldb::ModuleSP &module_sp, |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 298 | DataBufferSP& data_sp, |
| 299 | lldb::offset_t data_offset, |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 300 | const FileSpec* file, |
Greg Clayton | 5ce9c56 | 2013-02-06 17:22:03 +0000 | [diff] [blame] | 301 | lldb::offset_t file_offset, |
| 302 | lldb::offset_t length) : |
| 303 | ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset), |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 304 | m_header(), |
| 305 | m_program_headers(), |
| 306 | m_section_headers(), |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 307 | m_filespec_ap(), |
| 308 | m_shstr_data() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 309 | { |
| 310 | if (file) |
| 311 | m_file = *file; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 312 | ::memset(&m_header, 0, sizeof(m_header)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 315 | ObjectFileELF::~ObjectFileELF() |
| 316 | { |
| 317 | } |
| 318 | |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 319 | bool |
| 320 | ObjectFileELF::IsExecutable() const |
| 321 | { |
Stephen Wilson | 7f3b57c | 2011-01-15 00:09:50 +0000 | [diff] [blame] | 322 | return m_header.e_entry != 0; |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 325 | ByteOrder |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 326 | ObjectFileELF::GetByteOrder() const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 327 | { |
| 328 | if (m_header.e_ident[EI_DATA] == ELFDATA2MSB) |
| 329 | return eByteOrderBig; |
| 330 | if (m_header.e_ident[EI_DATA] == ELFDATA2LSB) |
| 331 | return eByteOrderLittle; |
| 332 | return eByteOrderInvalid; |
| 333 | } |
| 334 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 335 | uint32_t |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 336 | ObjectFileELF::GetAddressByteSize() const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 337 | { |
| 338 | return m_data.GetAddressByteSize(); |
| 339 | } |
| 340 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 341 | size_t |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 342 | ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 343 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 344 | return std::distance(m_section_headers.begin(), I) + 1u; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 345 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 346 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 347 | size_t |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 348 | ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const |
| 349 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 350 | return std::distance(m_section_headers.begin(), I) + 1u; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | bool |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 354 | ObjectFileELF::ParseHeader() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 355 | { |
Filipe Cabecinhas | 22b40f7 | 2013-05-16 23:29:36 +0000 | [diff] [blame] | 356 | lldb::offset_t offset = 0; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 357 | return m_header.Parse(m_data, &offset); |
| 358 | } |
| 359 | |
| 360 | bool |
Greg Clayton | 6083026 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 361 | ObjectFileELF::GetUUID(lldb_private::UUID* uuid) |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 362 | { |
| 363 | // FIXME: Return MD5 sum here. See comment in ObjectFile.h. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 364 | return false; |
| 365 | } |
| 366 | |
| 367 | uint32_t |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 368 | ObjectFileELF::GetDependentModules(FileSpecList &files) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 369 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 370 | size_t num_modules = ParseDependentModules(); |
| 371 | uint32_t num_specs = 0; |
| 372 | |
| 373 | for (unsigned i = 0; i < num_modules; ++i) |
| 374 | { |
| 375 | if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i))) |
| 376 | num_specs++; |
| 377 | } |
| 378 | |
| 379 | return num_specs; |
| 380 | } |
| 381 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 382 | user_id_t |
| 383 | ObjectFileELF::GetSectionIndexByType(unsigned type) |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 384 | { |
| 385 | if (!ParseSectionHeaders()) |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 386 | return 0; |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 387 | |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 388 | for (SectionHeaderCollIter sh_pos = m_section_headers.begin(); |
| 389 | sh_pos != m_section_headers.end(); ++sh_pos) |
| 390 | { |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 391 | if (sh_pos->sh_type == type) |
| 392 | return SectionIndex(sh_pos); |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | Address |
| 399 | ObjectFileELF::GetImageInfoAddress() |
| 400 | { |
| 401 | if (!ParseDynamicSymbols()) |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 402 | return Address(); |
| 403 | |
| 404 | SectionList *section_list = GetSectionList(); |
| 405 | if (!section_list) |
| 406 | return Address(); |
| 407 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 408 | user_id_t dynsym_id = GetSectionIndexByType(SHT_DYNAMIC); |
| 409 | if (!dynsym_id) |
| 410 | return Address(); |
| 411 | |
| 412 | const ELFSectionHeader *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id); |
| 413 | if (!dynsym_hdr) |
| 414 | return Address(); |
| 415 | |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 416 | SectionSP dynsym_section_sp (section_list->FindSectionByID(dynsym_id)); |
| 417 | if (dynsym_section_sp) |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 418 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 419 | for (size_t i = 0; i < m_dynamic_symbols.size(); ++i) |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 420 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 421 | ELFDynamic &symbol = m_dynamic_symbols[i]; |
| 422 | |
| 423 | if (symbol.d_tag == DT_DEBUG) |
| 424 | { |
| 425 | // Compute the offset as the number of previous entries plus the |
| 426 | // size of d_tag. |
| 427 | addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize(); |
| 428 | return Address(dynsym_section_sp, offset); |
| 429 | } |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
| 433 | return Address(); |
| 434 | } |
| 435 | |
Jim Ingham | 672e6f5 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 436 | lldb_private::Address |
| 437 | ObjectFileELF::GetEntryPointAddress () |
| 438 | { |
Stephen Wilson | d126c8c | 2011-03-08 04:12:15 +0000 | [diff] [blame] | 439 | SectionList *sections; |
| 440 | addr_t offset; |
Jim Ingham | 672e6f5 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 441 | |
Stephen Wilson | d126c8c | 2011-03-08 04:12:15 +0000 | [diff] [blame] | 442 | if (m_entry_point_address.IsValid()) |
| 443 | return m_entry_point_address; |
| 444 | |
| 445 | if (!ParseHeader() || !IsExecutable()) |
| 446 | return m_entry_point_address; |
| 447 | |
| 448 | sections = GetSectionList(); |
| 449 | offset = m_header.e_entry; |
| 450 | |
| 451 | if (!sections) |
| 452 | { |
| 453 | m_entry_point_address.SetOffset(offset); |
| 454 | return m_entry_point_address; |
| 455 | } |
| 456 | |
| 457 | m_entry_point_address.ResolveAddressUsingFileSections(offset, sections); |
| 458 | |
| 459 | return m_entry_point_address; |
Jim Ingham | 672e6f5 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 462 | //---------------------------------------------------------------------- |
| 463 | // ParseDependentModules |
| 464 | //---------------------------------------------------------------------- |
| 465 | size_t |
| 466 | ObjectFileELF::ParseDependentModules() |
| 467 | { |
| 468 | if (m_filespec_ap.get()) |
| 469 | return m_filespec_ap->GetSize(); |
| 470 | |
| 471 | m_filespec_ap.reset(new FileSpecList()); |
| 472 | |
| 473 | if (!(ParseSectionHeaders() && GetSectionHeaderStringTable())) |
| 474 | return 0; |
| 475 | |
| 476 | // Locate the dynamic table. |
| 477 | user_id_t dynsym_id = 0; |
| 478 | user_id_t dynstr_id = 0; |
Greg Clayton | 450e3f3 | 2010-10-12 02:24:53 +0000 | [diff] [blame] | 479 | for (SectionHeaderCollIter sh_pos = m_section_headers.begin(); |
| 480 | sh_pos != m_section_headers.end(); ++sh_pos) |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 481 | { |
Greg Clayton | 450e3f3 | 2010-10-12 02:24:53 +0000 | [diff] [blame] | 482 | if (sh_pos->sh_type == SHT_DYNAMIC) |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 483 | { |
Greg Clayton | 450e3f3 | 2010-10-12 02:24:53 +0000 | [diff] [blame] | 484 | dynsym_id = SectionIndex(sh_pos); |
| 485 | dynstr_id = sh_pos->sh_link + 1; // Section ID's are 1 based. |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 486 | break; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | if (!(dynsym_id && dynstr_id)) |
| 491 | return 0; |
| 492 | |
| 493 | SectionList *section_list = GetSectionList(); |
| 494 | if (!section_list) |
| 495 | return 0; |
| 496 | |
| 497 | // Resolve and load the dynamic table entries and corresponding string |
| 498 | // table. |
| 499 | Section *dynsym = section_list->FindSectionByID(dynsym_id).get(); |
| 500 | Section *dynstr = section_list->FindSectionByID(dynstr_id).get(); |
| 501 | if (!(dynsym && dynstr)) |
| 502 | return 0; |
| 503 | |
| 504 | DataExtractor dynsym_data; |
| 505 | DataExtractor dynstr_data; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 506 | if (ReadSectionData(dynsym, dynsym_data) && |
| 507 | ReadSectionData(dynstr, dynstr_data)) |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 508 | { |
| 509 | ELFDynamic symbol; |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 510 | const lldb::offset_t section_size = dynsym_data.GetByteSize(); |
| 511 | lldb::offset_t offset = 0; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 512 | |
| 513 | // The only type of entries we are concerned with are tagged DT_NEEDED, |
| 514 | // yielding the name of a required library. |
| 515 | while (offset < section_size) |
| 516 | { |
| 517 | if (!symbol.Parse(dynsym_data, &offset)) |
| 518 | break; |
| 519 | |
| 520 | if (symbol.d_tag != DT_NEEDED) |
| 521 | continue; |
| 522 | |
| 523 | uint32_t str_index = static_cast<uint32_t>(symbol.d_val); |
| 524 | const char *lib_name = dynstr_data.PeekCStr(str_index); |
Greg Clayton | 274060b | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 525 | m_filespec_ap->Append(FileSpec(lib_name, true)); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
| 529 | return m_filespec_ap->GetSize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | //---------------------------------------------------------------------- |
| 533 | // ParseProgramHeaders |
| 534 | //---------------------------------------------------------------------- |
| 535 | size_t |
| 536 | ObjectFileELF::ParseProgramHeaders() |
| 537 | { |
| 538 | // We have already parsed the program headers |
| 539 | if (!m_program_headers.empty()) |
| 540 | return m_program_headers.size(); |
| 541 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 542 | // If there are no program headers to read we are done. |
| 543 | if (m_header.e_phnum == 0) |
| 544 | return 0; |
| 545 | |
| 546 | m_program_headers.resize(m_header.e_phnum); |
| 547 | if (m_program_headers.size() != m_header.e_phnum) |
| 548 | return 0; |
| 549 | |
| 550 | const size_t ph_size = m_header.e_phnum * m_header.e_phentsize; |
Greg Clayton | 44435ed | 2012-01-12 05:25:17 +0000 | [diff] [blame] | 551 | const elf_off ph_offset = m_header.e_phoff; |
| 552 | DataExtractor data; |
| 553 | if (GetData (ph_offset, ph_size, data) != ph_size) |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 554 | return 0; |
| 555 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 556 | uint32_t idx; |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 557 | lldb::offset_t offset; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 558 | for (idx = 0, offset = 0; idx < m_header.e_phnum; ++idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 559 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 560 | if (m_program_headers[idx].Parse(data, &offset) == false) |
| 561 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 564 | if (idx < m_program_headers.size()) |
| 565 | m_program_headers.resize(idx); |
| 566 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 567 | return m_program_headers.size(); |
| 568 | } |
| 569 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 570 | //---------------------------------------------------------------------- |
| 571 | // ParseSectionHeaders |
| 572 | //---------------------------------------------------------------------- |
| 573 | size_t |
| 574 | ObjectFileELF::ParseSectionHeaders() |
| 575 | { |
| 576 | // We have already parsed the section headers |
| 577 | if (!m_section_headers.empty()) |
| 578 | return m_section_headers.size(); |
| 579 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 580 | // If there are no section headers we are done. |
| 581 | if (m_header.e_shnum == 0) |
| 582 | return 0; |
| 583 | |
| 584 | m_section_headers.resize(m_header.e_shnum); |
| 585 | if (m_section_headers.size() != m_header.e_shnum) |
| 586 | return 0; |
| 587 | |
| 588 | const size_t sh_size = m_header.e_shnum * m_header.e_shentsize; |
Greg Clayton | 44435ed | 2012-01-12 05:25:17 +0000 | [diff] [blame] | 589 | const elf_off sh_offset = m_header.e_shoff; |
| 590 | DataExtractor data; |
| 591 | if (GetData (sh_offset, sh_size, data) != sh_size) |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 592 | return 0; |
| 593 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 594 | uint32_t idx; |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 595 | lldb::offset_t offset; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 596 | for (idx = 0, offset = 0; idx < m_header.e_shnum; ++idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 597 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 598 | if (m_section_headers[idx].Parse(data, &offset) == false) |
| 599 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 600 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 601 | if (idx < m_section_headers.size()) |
| 602 | m_section_headers.resize(idx); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 603 | |
| 604 | return m_section_headers.size(); |
| 605 | } |
| 606 | |
| 607 | size_t |
| 608 | ObjectFileELF::GetSectionHeaderStringTable() |
| 609 | { |
| 610 | if (m_shstr_data.GetByteSize() == 0) |
| 611 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 612 | const unsigned strtab_idx = m_header.e_shstrndx; |
| 613 | |
| 614 | if (strtab_idx && strtab_idx < m_section_headers.size()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 615 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 616 | const ELFSectionHeader &sheader = m_section_headers[strtab_idx]; |
| 617 | const size_t byte_size = sheader.sh_size; |
Greg Clayton | 44435ed | 2012-01-12 05:25:17 +0000 | [diff] [blame] | 618 | const Elf64_Off offset = sheader.sh_offset; |
| 619 | m_shstr_data.SetData (m_data, offset, byte_size); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 620 | |
Greg Clayton | 44435ed | 2012-01-12 05:25:17 +0000 | [diff] [blame] | 621 | if (m_shstr_data.GetByteSize() != byte_size) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 622 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | return m_shstr_data.GetByteSize(); |
| 626 | } |
| 627 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 628 | lldb::user_id_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 629 | ObjectFileELF::GetSectionIndexByName(const char *name) |
| 630 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 631 | if (!(ParseSectionHeaders() && GetSectionHeaderStringTable())) |
| 632 | return 0; |
| 633 | |
| 634 | // Search the collection of section headers for one with a matching name. |
| 635 | for (SectionHeaderCollIter I = m_section_headers.begin(); |
| 636 | I != m_section_headers.end(); ++I) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 637 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 638 | const char *sectionName = m_shstr_data.PeekCStr(I->sh_name); |
| 639 | |
| 640 | if (!sectionName) |
| 641 | return 0; |
| 642 | |
| 643 | if (strcmp(name, sectionName) != 0) |
| 644 | continue; |
| 645 | |
| 646 | return SectionIndex(I); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 649 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 652 | const elf::ELFSectionHeader * |
| 653 | ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id) |
| 654 | { |
| 655 | if (!ParseSectionHeaders() || !id) |
| 656 | return NULL; |
| 657 | |
| 658 | if (--id < m_section_headers.size()) |
| 659 | return &m_section_headers[id]; |
| 660 | |
| 661 | return NULL; |
| 662 | } |
| 663 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 664 | SectionList * |
| 665 | ObjectFileELF::GetSectionList() |
| 666 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 667 | if (m_sections_ap.get()) |
| 668 | return m_sections_ap.get(); |
| 669 | |
| 670 | if (ParseSectionHeaders() && GetSectionHeaderStringTable()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 671 | { |
| 672 | m_sections_ap.reset(new SectionList()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 673 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 674 | for (SectionHeaderCollIter I = m_section_headers.begin(); |
| 675 | I != m_section_headers.end(); ++I) |
| 676 | { |
| 677 | const ELFSectionHeader &header = *I; |
| 678 | |
| 679 | ConstString name(m_shstr_data.PeekCStr(header.sh_name)); |
Greg Clayton | 47037bc | 2012-03-27 02:40:46 +0000 | [diff] [blame] | 680 | const uint64_t file_size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size; |
| 681 | 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] | 682 | |
Greg Clayton | 4ceb998 | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 683 | static ConstString g_sect_name_text (".text"); |
| 684 | static ConstString g_sect_name_data (".data"); |
| 685 | static ConstString g_sect_name_bss (".bss"); |
Greg Clayton | 741f3f9 | 2012-03-27 21:10:07 +0000 | [diff] [blame] | 686 | static ConstString g_sect_name_tdata (".tdata"); |
| 687 | static ConstString g_sect_name_tbss (".tbss"); |
Greg Clayton | 4ceb998 | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 688 | static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev"); |
| 689 | static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges"); |
| 690 | static ConstString g_sect_name_dwarf_debug_frame (".debug_frame"); |
| 691 | static ConstString g_sect_name_dwarf_debug_info (".debug_info"); |
| 692 | static ConstString g_sect_name_dwarf_debug_line (".debug_line"); |
| 693 | static ConstString g_sect_name_dwarf_debug_loc (".debug_loc"); |
| 694 | static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo"); |
| 695 | static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames"); |
| 696 | static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes"); |
| 697 | static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges"); |
| 698 | static ConstString g_sect_name_dwarf_debug_str (".debug_str"); |
| 699 | static ConstString g_sect_name_eh_frame (".eh_frame"); |
| 700 | |
| 701 | SectionType sect_type = eSectionTypeOther; |
| 702 | |
Greg Clayton | 741f3f9 | 2012-03-27 21:10:07 +0000 | [diff] [blame] | 703 | bool is_thread_specific = false; |
| 704 | |
Greg Clayton | 4ceb998 | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 705 | if (name == g_sect_name_text) sect_type = eSectionTypeCode; |
| 706 | else if (name == g_sect_name_data) sect_type = eSectionTypeData; |
| 707 | else if (name == g_sect_name_bss) sect_type = eSectionTypeZeroFill; |
Greg Clayton | 741f3f9 | 2012-03-27 21:10:07 +0000 | [diff] [blame] | 708 | else if (name == g_sect_name_tdata) |
| 709 | { |
| 710 | sect_type = eSectionTypeData; |
| 711 | is_thread_specific = true; |
| 712 | } |
| 713 | else if (name == g_sect_name_tbss) |
| 714 | { |
| 715 | sect_type = eSectionTypeZeroFill; |
| 716 | is_thread_specific = true; |
| 717 | } |
Greg Clayton | 4ceb998 | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 718 | else if (name == g_sect_name_dwarf_debug_abbrev) sect_type = eSectionTypeDWARFDebugAbbrev; |
| 719 | else if (name == g_sect_name_dwarf_debug_aranges) sect_type = eSectionTypeDWARFDebugAranges; |
| 720 | else if (name == g_sect_name_dwarf_debug_frame) sect_type = eSectionTypeDWARFDebugFrame; |
| 721 | else if (name == g_sect_name_dwarf_debug_info) sect_type = eSectionTypeDWARFDebugInfo; |
| 722 | else if (name == g_sect_name_dwarf_debug_line) sect_type = eSectionTypeDWARFDebugLine; |
| 723 | else if (name == g_sect_name_dwarf_debug_loc) sect_type = eSectionTypeDWARFDebugLoc; |
| 724 | else if (name == g_sect_name_dwarf_debug_macinfo) sect_type = eSectionTypeDWARFDebugMacInfo; |
| 725 | else if (name == g_sect_name_dwarf_debug_pubnames) sect_type = eSectionTypeDWARFDebugPubNames; |
| 726 | else if (name == g_sect_name_dwarf_debug_pubtypes) sect_type = eSectionTypeDWARFDebugPubTypes; |
| 727 | else if (name == g_sect_name_dwarf_debug_ranges) sect_type = eSectionTypeDWARFDebugRanges; |
| 728 | else if (name == g_sect_name_dwarf_debug_str) sect_type = eSectionTypeDWARFDebugStr; |
| 729 | else if (name == g_sect_name_eh_frame) sect_type = eSectionTypeEHFrame; |
| 730 | |
| 731 | |
Greg Clayton | 741f3f9 | 2012-03-27 21:10:07 +0000 | [diff] [blame] | 732 | SectionSP section_sp(new Section( |
Greg Clayton | 4ceb998 | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 733 | GetModule(), // Module to which this section belongs. |
| 734 | SectionIndex(I), // Section ID. |
| 735 | name, // Section name. |
| 736 | sect_type, // Section type. |
| 737 | header.sh_addr, // VM address. |
Greg Clayton | 47037bc | 2012-03-27 02:40:46 +0000 | [diff] [blame] | 738 | vm_size, // VM size in bytes of this section. |
Greg Clayton | 4ceb998 | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 739 | header.sh_offset, // Offset of this section in the file. |
Greg Clayton | 47037bc | 2012-03-27 02:40:46 +0000 | [diff] [blame] | 740 | file_size, // Size of the section as found in the file. |
Greg Clayton | 4ceb998 | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 741 | header.sh_flags)); // Flags for this section. |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 742 | |
Greg Clayton | 741f3f9 | 2012-03-27 21:10:07 +0000 | [diff] [blame] | 743 | if (is_thread_specific) |
| 744 | section_sp->SetIsThreadSpecific (is_thread_specific); |
| 745 | m_sections_ap->AddSection(section_sp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 746 | } |
Sean Callanan | 5677536 | 2012-06-08 02:16:08 +0000 | [diff] [blame] | 747 | |
| 748 | m_sections_ap->Finalize(); // Now that we're done adding sections, finalize to build fast-lookup caches |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 749 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 750 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 751 | return m_sections_ap.get(); |
| 752 | } |
| 753 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 754 | static unsigned |
| 755 | ParseSymbols(Symtab *symtab, |
| 756 | user_id_t start_id, |
| 757 | SectionList *section_list, |
| 758 | const ELFSectionHeader *symtab_shdr, |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 759 | const DataExtractor &symtab_data, |
| 760 | const DataExtractor &strtab_data) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 761 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 762 | ELFSymbol symbol; |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 763 | lldb::offset_t offset = 0; |
| 764 | const size_t num_symbols = symtab_data.GetByteSize() / symtab_shdr->sh_entsize; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 765 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 766 | static ConstString text_section_name(".text"); |
| 767 | static ConstString init_section_name(".init"); |
| 768 | static ConstString fini_section_name(".fini"); |
| 769 | static ConstString ctors_section_name(".ctors"); |
| 770 | static ConstString dtors_section_name(".dtors"); |
| 771 | |
| 772 | static ConstString data_section_name(".data"); |
| 773 | static ConstString rodata_section_name(".rodata"); |
| 774 | static ConstString rodata1_section_name(".rodata1"); |
| 775 | static ConstString data2_section_name(".data1"); |
| 776 | static ConstString bss_section_name(".bss"); |
| 777 | |
Greg Clayton | 9594f4c | 2013-04-13 23:17:23 +0000 | [diff] [blame] | 778 | //StreamFile strm(stdout, false); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 779 | unsigned i; |
| 780 | for (i = 0; i < num_symbols; ++i) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 781 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 782 | if (symbol.Parse(symtab_data, &offset) == false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 783 | break; |
Greg Clayton | 9594f4c | 2013-04-13 23:17:23 +0000 | [diff] [blame] | 784 | |
| 785 | const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); |
| 786 | |
| 787 | // No need to add symbols that have no names |
| 788 | if (symbol_name == NULL || symbol_name[0] == '\0') |
| 789 | continue; |
| 790 | |
| 791 | //symbol.Dump (&strm, i, &strtab_data, section_list); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 792 | |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 793 | SectionSP symbol_section_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 794 | SymbolType symbol_type = eSymbolTypeInvalid; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 795 | Elf64_Half symbol_idx = symbol.st_shndx; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 796 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 797 | switch (symbol_idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 798 | { |
| 799 | case SHN_ABS: |
| 800 | symbol_type = eSymbolTypeAbsolute; |
| 801 | break; |
| 802 | case SHN_UNDEF: |
| 803 | symbol_type = eSymbolTypeUndefined; |
| 804 | break; |
| 805 | default: |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 806 | symbol_section_sp = section_list->GetSectionAtIndex(symbol_idx); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 807 | break; |
| 808 | } |
| 809 | |
Matt Kopec | 92dd5cf | 2013-02-12 18:30:30 +0000 | [diff] [blame] | 810 | // If a symbol is undefined do not process it further even if it has a STT type |
| 811 | if (symbol_type != eSymbolTypeUndefined) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 812 | { |
Matt Kopec | 92dd5cf | 2013-02-12 18:30:30 +0000 | [diff] [blame] | 813 | switch (symbol.getType()) |
| 814 | { |
| 815 | default: |
| 816 | case STT_NOTYPE: |
| 817 | // The symbol's type is not specified. |
| 818 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 819 | |
Matt Kopec | 92dd5cf | 2013-02-12 18:30:30 +0000 | [diff] [blame] | 820 | case STT_OBJECT: |
| 821 | // The symbol is associated with a data object, such as a variable, |
| 822 | // an array, etc. |
| 823 | symbol_type = eSymbolTypeData; |
| 824 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 825 | |
Matt Kopec | 92dd5cf | 2013-02-12 18:30:30 +0000 | [diff] [blame] | 826 | case STT_FUNC: |
| 827 | // The symbol is associated with a function or other executable code. |
| 828 | symbol_type = eSymbolTypeCode; |
| 829 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 830 | |
Matt Kopec | 92dd5cf | 2013-02-12 18:30:30 +0000 | [diff] [blame] | 831 | case STT_SECTION: |
| 832 | // The symbol is associated with a section. Symbol table entries of |
| 833 | // this type exist primarily for relocation and normally have |
| 834 | // STB_LOCAL binding. |
| 835 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 836 | |
Matt Kopec | 92dd5cf | 2013-02-12 18:30:30 +0000 | [diff] [blame] | 837 | case STT_FILE: |
| 838 | // Conventionally, the symbol's name gives the name of the source |
| 839 | // file associated with the object file. A file symbol has STB_LOCAL |
| 840 | // binding, its section index is SHN_ABS, and it precedes the other |
| 841 | // STB_LOCAL symbols for the file, if it is present. |
Greg Clayton | 9594f4c | 2013-04-13 23:17:23 +0000 | [diff] [blame] | 842 | symbol_type = eSymbolTypeSourceFile; |
Matt Kopec | 92dd5cf | 2013-02-12 18:30:30 +0000 | [diff] [blame] | 843 | break; |
Matt Kopec | 00049b8 | 2013-02-27 20:13:38 +0000 | [diff] [blame] | 844 | |
| 845 | case STT_GNU_IFUNC: |
| 846 | // The symbol is associated with an indirect function. The actual |
| 847 | // function will be resolved if it is referenced. |
| 848 | symbol_type = eSymbolTypeResolver; |
| 849 | break; |
Matt Kopec | 92dd5cf | 2013-02-12 18:30:30 +0000 | [diff] [blame] | 850 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | if (symbol_type == eSymbolTypeInvalid) |
| 854 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 855 | if (symbol_section_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 856 | { |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 857 | const ConstString §_name = symbol_section_sp->GetName(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 858 | if (sect_name == text_section_name || |
| 859 | sect_name == init_section_name || |
| 860 | sect_name == fini_section_name || |
| 861 | sect_name == ctors_section_name || |
| 862 | sect_name == dtors_section_name) |
| 863 | { |
| 864 | symbol_type = eSymbolTypeCode; |
| 865 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 866 | else if (sect_name == data_section_name || |
| 867 | sect_name == data2_section_name || |
| 868 | sect_name == rodata_section_name || |
| 869 | sect_name == rodata1_section_name || |
| 870 | sect_name == bss_section_name) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 871 | { |
| 872 | symbol_type = eSymbolTypeData; |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | uint64_t symbol_value = symbol.st_value; |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 878 | if (symbol_section_sp) |
| 879 | symbol_value -= symbol_section_sp->GetFileAddress(); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 880 | bool is_global = symbol.getBinding() == STB_GLOBAL; |
| 881 | uint32_t flags = symbol.st_other << 8 | symbol.st_info; |
Greg Clayton | 47037bc | 2012-03-27 02:40:46 +0000 | [diff] [blame] | 882 | 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] | 883 | Symbol dc_symbol( |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 884 | i + start_id, // ID is the original symbol table index. |
| 885 | symbol_name, // Symbol name. |
Greg Clayton | 47037bc | 2012-03-27 02:40:46 +0000 | [diff] [blame] | 886 | is_mangled, // Is the symbol name mangled? |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 887 | symbol_type, // Type of this symbol |
| 888 | is_global, // Is this globally visible? |
| 889 | false, // Is this symbol debug info? |
| 890 | false, // Is this symbol a trampoline? |
| 891 | false, // Is this symbol artificial? |
| 892 | symbol_section_sp, // Section in which this symbol is defined or null. |
| 893 | symbol_value, // Offset in section or symbol value. |
| 894 | symbol.st_size, // Size in bytes of this symbol. |
Greg Clayton | 9594f4c | 2013-04-13 23:17:23 +0000 | [diff] [blame] | 895 | true, // Size is valid |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 896 | flags); // Symbol flags. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 897 | symtab->AddSymbol(dc_symbol); |
| 898 | } |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 899 | |
| 900 | return i; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 903 | unsigned |
| 904 | ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, user_id_t start_id, |
| 905 | const ELFSectionHeader *symtab_hdr, |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 906 | user_id_t symtab_id) |
| 907 | { |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 908 | assert(symtab_hdr->sh_type == SHT_SYMTAB || |
| 909 | symtab_hdr->sh_type == SHT_DYNSYM); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 910 | |
| 911 | // Parse in the section list if needed. |
| 912 | SectionList *section_list = GetSectionList(); |
| 913 | if (!section_list) |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 914 | return 0; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 915 | |
| 916 | // Section ID's are ones based. |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 917 | user_id_t strtab_id = symtab_hdr->sh_link + 1; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 918 | |
| 919 | Section *symtab = section_list->FindSectionByID(symtab_id).get(); |
| 920 | Section *strtab = section_list->FindSectionByID(strtab_id).get(); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 921 | unsigned num_symbols = 0; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 922 | if (symtab && strtab) |
| 923 | { |
| 924 | DataExtractor symtab_data; |
| 925 | DataExtractor strtab_data; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 926 | if (ReadSectionData(symtab, symtab_data) && |
| 927 | ReadSectionData(strtab, strtab_data)) |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 928 | { |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 929 | num_symbols = ParseSymbols(symbol_table, start_id, |
| 930 | section_list, symtab_hdr, |
| 931 | symtab_data, strtab_data); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 932 | } |
| 933 | } |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 934 | |
| 935 | return num_symbols; |
| 936 | } |
| 937 | |
| 938 | size_t |
| 939 | ObjectFileELF::ParseDynamicSymbols() |
| 940 | { |
| 941 | if (m_dynamic_symbols.size()) |
| 942 | return m_dynamic_symbols.size(); |
| 943 | |
| 944 | user_id_t dyn_id = GetSectionIndexByType(SHT_DYNAMIC); |
| 945 | if (!dyn_id) |
Bill Wendling | ed24dcc | 2012-04-03 07:50:11 +0000 | [diff] [blame] | 946 | return 0; |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 947 | |
| 948 | SectionList *section_list = GetSectionList(); |
| 949 | if (!section_list) |
Bill Wendling | ed24dcc | 2012-04-03 07:50:11 +0000 | [diff] [blame] | 950 | return 0; |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 951 | |
| 952 | Section *dynsym = section_list->FindSectionByID(dyn_id).get(); |
| 953 | if (!dynsym) |
Bill Wendling | ed24dcc | 2012-04-03 07:50:11 +0000 | [diff] [blame] | 954 | return 0; |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 955 | |
| 956 | ELFDynamic symbol; |
| 957 | DataExtractor dynsym_data; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 958 | if (ReadSectionData(dynsym, dynsym_data)) |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 959 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 960 | const lldb::offset_t section_size = dynsym_data.GetByteSize(); |
| 961 | lldb::offset_t cursor = 0; |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 962 | |
| 963 | while (cursor < section_size) |
| 964 | { |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 965 | if (!symbol.Parse(dynsym_data, &cursor)) |
| 966 | break; |
| 967 | |
| 968 | m_dynamic_symbols.push_back(symbol); |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | return m_dynamic_symbols.size(); |
| 973 | } |
| 974 | |
| 975 | const ELFDynamic * |
| 976 | ObjectFileELF::FindDynamicSymbol(unsigned tag) |
| 977 | { |
| 978 | if (!ParseDynamicSymbols()) |
| 979 | return NULL; |
| 980 | |
| 981 | SectionList *section_list = GetSectionList(); |
| 982 | if (!section_list) |
| 983 | return 0; |
| 984 | |
| 985 | DynamicSymbolCollIter I = m_dynamic_symbols.begin(); |
| 986 | DynamicSymbolCollIter E = m_dynamic_symbols.end(); |
| 987 | for ( ; I != E; ++I) |
| 988 | { |
| 989 | ELFDynamic *symbol = &*I; |
| 990 | |
| 991 | if (symbol->d_tag == tag) |
| 992 | return symbol; |
| 993 | } |
| 994 | |
| 995 | return NULL; |
| 996 | } |
| 997 | |
| 998 | Section * |
| 999 | ObjectFileELF::PLTSection() |
| 1000 | { |
| 1001 | const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL); |
| 1002 | SectionList *section_list = GetSectionList(); |
| 1003 | |
| 1004 | if (symbol && section_list) |
| 1005 | { |
| 1006 | addr_t addr = symbol->d_ptr; |
| 1007 | return section_list->FindSectionContainingFileAddress(addr).get(); |
| 1008 | } |
| 1009 | |
| 1010 | return NULL; |
| 1011 | } |
| 1012 | |
| 1013 | unsigned |
| 1014 | ObjectFileELF::PLTRelocationType() |
| 1015 | { |
| 1016 | const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL); |
| 1017 | |
| 1018 | if (symbol) |
| 1019 | return symbol->d_val; |
| 1020 | |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | static unsigned |
| 1025 | ParsePLTRelocations(Symtab *symbol_table, |
| 1026 | user_id_t start_id, |
| 1027 | unsigned rel_type, |
| 1028 | const ELFHeader *hdr, |
| 1029 | const ELFSectionHeader *rel_hdr, |
| 1030 | const ELFSectionHeader *plt_hdr, |
| 1031 | const ELFSectionHeader *sym_hdr, |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1032 | const lldb::SectionSP &plt_section_sp, |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1033 | DataExtractor &rel_data, |
| 1034 | DataExtractor &symtab_data, |
| 1035 | DataExtractor &strtab_data) |
| 1036 | { |
| 1037 | ELFRelocation rel(rel_type); |
| 1038 | ELFSymbol symbol; |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1039 | lldb::offset_t offset = 0; |
| 1040 | const elf_xword plt_entsize = plt_hdr->sh_entsize; |
| 1041 | 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] | 1042 | |
| 1043 | typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel); |
| 1044 | reloc_info_fn reloc_type; |
| 1045 | reloc_info_fn reloc_symbol; |
| 1046 | |
Greg Clayton | d091afe | 2012-11-12 22:53:16 +0000 | [diff] [blame] | 1047 | if (hdr->Is32Bit()) |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1048 | { |
| 1049 | reloc_type = ELFRelocation::RelocType32; |
| 1050 | reloc_symbol = ELFRelocation::RelocSymbol32; |
| 1051 | } |
| 1052 | else |
| 1053 | { |
| 1054 | reloc_type = ELFRelocation::RelocType64; |
| 1055 | reloc_symbol = ELFRelocation::RelocSymbol64; |
| 1056 | } |
| 1057 | |
| 1058 | unsigned slot_type = hdr->GetRelocationJumpSlotType(); |
| 1059 | unsigned i; |
| 1060 | for (i = 0; i < num_relocations; ++i) |
| 1061 | { |
| 1062 | if (rel.Parse(rel_data, &offset) == false) |
| 1063 | break; |
| 1064 | |
| 1065 | if (reloc_type(rel) != slot_type) |
| 1066 | continue; |
| 1067 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1068 | lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize; |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1069 | uint64_t plt_index = (i + 1) * plt_entsize; |
| 1070 | |
| 1071 | if (!symbol.Parse(symtab_data, &symbol_offset)) |
| 1072 | break; |
| 1073 | |
| 1074 | const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); |
Greg Clayton | 47037bc | 2012-03-27 02:40:46 +0000 | [diff] [blame] | 1075 | 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] | 1076 | |
| 1077 | Symbol jump_symbol( |
| 1078 | i + start_id, // Symbol table index |
| 1079 | symbol_name, // symbol name. |
Greg Clayton | 47037bc | 2012-03-27 02:40:46 +0000 | [diff] [blame] | 1080 | is_mangled, // is the symbol name mangled? |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1081 | eSymbolTypeTrampoline, // Type of this symbol |
| 1082 | false, // Is this globally visible? |
| 1083 | false, // Is this symbol debug info? |
| 1084 | true, // Is this symbol a trampoline? |
| 1085 | true, // Is this symbol artificial? |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1086 | plt_section_sp, // Section in which this symbol is defined or null. |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1087 | plt_index, // Offset in section or symbol value. |
| 1088 | plt_entsize, // Size in bytes of this symbol. |
Greg Clayton | 9594f4c | 2013-04-13 23:17:23 +0000 | [diff] [blame] | 1089 | true, // Size is valid |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1090 | 0); // Symbol flags. |
| 1091 | |
| 1092 | symbol_table->AddSymbol(jump_symbol); |
| 1093 | } |
| 1094 | |
| 1095 | return i; |
| 1096 | } |
| 1097 | |
| 1098 | unsigned |
| 1099 | ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table, |
| 1100 | user_id_t start_id, |
| 1101 | const ELFSectionHeader *rel_hdr, |
| 1102 | user_id_t rel_id) |
| 1103 | { |
| 1104 | assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL); |
| 1105 | |
| 1106 | // The link field points to the asscoiated symbol table. The info field |
| 1107 | // points to the section holding the plt. |
| 1108 | user_id_t symtab_id = rel_hdr->sh_link; |
| 1109 | user_id_t plt_id = rel_hdr->sh_info; |
| 1110 | |
| 1111 | if (!symtab_id || !plt_id) |
| 1112 | return 0; |
| 1113 | |
| 1114 | // Section ID's are ones based; |
| 1115 | symtab_id++; |
| 1116 | plt_id++; |
| 1117 | |
| 1118 | const ELFSectionHeader *plt_hdr = GetSectionHeaderByIndex(plt_id); |
| 1119 | if (!plt_hdr) |
| 1120 | return 0; |
| 1121 | |
| 1122 | const ELFSectionHeader *sym_hdr = GetSectionHeaderByIndex(symtab_id); |
| 1123 | if (!sym_hdr) |
| 1124 | return 0; |
| 1125 | |
| 1126 | SectionList *section_list = GetSectionList(); |
| 1127 | if (!section_list) |
| 1128 | return 0; |
| 1129 | |
| 1130 | Section *rel_section = section_list->FindSectionByID(rel_id).get(); |
| 1131 | if (!rel_section) |
| 1132 | return 0; |
| 1133 | |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1134 | SectionSP plt_section_sp (section_list->FindSectionByID(plt_id)); |
| 1135 | if (!plt_section_sp) |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1136 | return 0; |
| 1137 | |
| 1138 | Section *symtab = section_list->FindSectionByID(symtab_id).get(); |
| 1139 | if (!symtab) |
| 1140 | return 0; |
| 1141 | |
| 1142 | Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get(); |
| 1143 | if (!strtab) |
| 1144 | return 0; |
| 1145 | |
| 1146 | DataExtractor rel_data; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 1147 | if (!ReadSectionData(rel_section, rel_data)) |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1148 | return 0; |
| 1149 | |
| 1150 | DataExtractor symtab_data; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 1151 | if (!ReadSectionData(symtab, symtab_data)) |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1152 | return 0; |
| 1153 | |
| 1154 | DataExtractor strtab_data; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 1155 | if (!ReadSectionData(strtab, strtab_data)) |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1156 | return 0; |
| 1157 | |
| 1158 | unsigned rel_type = PLTRelocationType(); |
| 1159 | if (!rel_type) |
| 1160 | return 0; |
| 1161 | |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 1162 | return ParsePLTRelocations (symbol_table, |
| 1163 | start_id, |
| 1164 | rel_type, |
| 1165 | &m_header, |
| 1166 | rel_hdr, |
| 1167 | plt_hdr, |
| 1168 | sym_hdr, |
| 1169 | plt_section_sp, |
| 1170 | rel_data, |
| 1171 | symtab_data, |
| 1172 | strtab_data); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1173 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1174 | |
| 1175 | Symtab * |
| 1176 | ObjectFileELF::GetSymtab() |
| 1177 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1178 | if (m_symtab_ap.get()) |
| 1179 | return m_symtab_ap.get(); |
| 1180 | |
| 1181 | Symtab *symbol_table = new Symtab(this); |
| 1182 | m_symtab_ap.reset(symbol_table); |
| 1183 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1184 | Mutex::Locker locker(symbol_table->GetMutex()); |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 1185 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1186 | if (!(ParseSectionHeaders() && GetSectionHeaderStringTable())) |
| 1187 | return symbol_table; |
| 1188 | |
| 1189 | // Locate and parse all linker symbol tables. |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1190 | uint64_t symbol_id = 0; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1191 | for (SectionHeaderCollIter I = m_section_headers.begin(); |
| 1192 | I != m_section_headers.end(); ++I) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1193 | { |
Peter Collingbourne | b4aabeb | 2011-06-03 20:39:58 +0000 | [diff] [blame] | 1194 | if (I->sh_type == SHT_SYMTAB || I->sh_type == SHT_DYNSYM) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1195 | { |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1196 | const ELFSectionHeader &symtab_header = *I; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1197 | user_id_t section_id = SectionIndex(I); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1198 | symbol_id += ParseSymbolTable(symbol_table, symbol_id, |
| 1199 | &symtab_header, section_id); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1200 | } |
| 1201 | } |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 1202 | |
| 1203 | // Synthesize trampoline symbols to help navigate the PLT. |
| 1204 | Section *reloc_section = PLTSection(); |
| 1205 | if (reloc_section) |
| 1206 | { |
| 1207 | user_id_t reloc_id = reloc_section->GetID(); |
| 1208 | const ELFSectionHeader *reloc_header = GetSectionHeaderByIndex(reloc_id); |
| 1209 | assert(reloc_header); |
| 1210 | |
| 1211 | ParseTrampolineSymbols(symbol_table, symbol_id, reloc_header, reloc_id); |
| 1212 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1213 | |
| 1214 | return symbol_table; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1217 | //===----------------------------------------------------------------------===// |
| 1218 | // Dump |
| 1219 | // |
| 1220 | // Dump the specifics of the runtime file container (such as any headers |
| 1221 | // segments, sections, etc). |
| 1222 | //---------------------------------------------------------------------- |
| 1223 | void |
| 1224 | ObjectFileELF::Dump(Stream *s) |
| 1225 | { |
| 1226 | DumpELFHeader(s, m_header); |
| 1227 | s->EOL(); |
| 1228 | DumpELFProgramHeaders(s); |
| 1229 | s->EOL(); |
| 1230 | DumpELFSectionHeaders(s); |
| 1231 | s->EOL(); |
| 1232 | SectionList *section_list = GetSectionList(); |
| 1233 | if (section_list) |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 1234 | section_list->Dump(s, NULL, true, UINT32_MAX); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1235 | Symtab *symtab = GetSymtab(); |
| 1236 | if (symtab) |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 1237 | symtab->Dump(s, NULL, eSortOrderNone); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1238 | s->EOL(); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1239 | DumpDependentModules(s); |
| 1240 | s->EOL(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | //---------------------------------------------------------------------- |
| 1244 | // DumpELFHeader |
| 1245 | // |
| 1246 | // Dump the ELF header to the specified output stream |
| 1247 | //---------------------------------------------------------------------- |
| 1248 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1249 | ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1250 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1251 | s->PutCString("ELF Header\n"); |
| 1252 | s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]); |
| 1253 | s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n", |
| 1254 | header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]); |
| 1255 | s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n", |
| 1256 | header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]); |
| 1257 | s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n", |
| 1258 | header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1259 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1260 | s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]); |
| 1261 | 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] | 1262 | DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]); |
| 1263 | s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]); |
| 1264 | s->Printf ("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]); |
| 1265 | |
| 1266 | s->Printf("e_type = 0x%4.4x ", header.e_type); |
| 1267 | DumpELFHeader_e_type(s, header.e_type); |
| 1268 | s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine); |
| 1269 | s->Printf("e_version = 0x%8.8x\n", header.e_version); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1270 | s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry); |
| 1271 | s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff); |
| 1272 | s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1273 | s->Printf("e_flags = 0x%8.8x\n", header.e_flags); |
| 1274 | s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize); |
| 1275 | s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize); |
| 1276 | s->Printf("e_phnum = 0x%4.4x\n", header.e_phnum); |
| 1277 | s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize); |
| 1278 | s->Printf("e_shnum = 0x%4.4x\n", header.e_shnum); |
| 1279 | s->Printf("e_shstrndx = 0x%4.4x\n", header.e_shstrndx); |
| 1280 | } |
| 1281 | |
| 1282 | //---------------------------------------------------------------------- |
| 1283 | // DumpELFHeader_e_type |
| 1284 | // |
| 1285 | // Dump an token value for the ELF header member e_type |
| 1286 | //---------------------------------------------------------------------- |
| 1287 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1288 | ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1289 | { |
| 1290 | switch (e_type) |
| 1291 | { |
| 1292 | case ET_NONE: *s << "ET_NONE"; break; |
| 1293 | case ET_REL: *s << "ET_REL"; break; |
| 1294 | case ET_EXEC: *s << "ET_EXEC"; break; |
| 1295 | case ET_DYN: *s << "ET_DYN"; break; |
| 1296 | case ET_CORE: *s << "ET_CORE"; break; |
| 1297 | default: |
| 1298 | break; |
| 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | //---------------------------------------------------------------------- |
| 1303 | // DumpELFHeader_e_ident_EI_DATA |
| 1304 | // |
| 1305 | // Dump an token value for the ELF header member e_ident[EI_DATA] |
| 1306 | //---------------------------------------------------------------------- |
| 1307 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1308 | ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1309 | { |
| 1310 | switch (ei_data) |
| 1311 | { |
| 1312 | case ELFDATANONE: *s << "ELFDATANONE"; break; |
| 1313 | case ELFDATA2LSB: *s << "ELFDATA2LSB - Little Endian"; break; |
| 1314 | case ELFDATA2MSB: *s << "ELFDATA2MSB - Big Endian"; break; |
| 1315 | default: |
| 1316 | break; |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | |
| 1321 | //---------------------------------------------------------------------- |
| 1322 | // DumpELFProgramHeader |
| 1323 | // |
| 1324 | // Dump a single ELF program header to the specified output stream |
| 1325 | //---------------------------------------------------------------------- |
| 1326 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1327 | ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1328 | { |
| 1329 | DumpELFProgramHeader_p_type(s, ph.p_type); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1330 | s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset, ph.p_vaddr, ph.p_paddr); |
| 1331 | 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] | 1332 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1333 | DumpELFProgramHeader_p_flags(s, ph.p_flags); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1334 | s->Printf(") %8.8" PRIx64, ph.p_align); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | //---------------------------------------------------------------------- |
| 1338 | // DumpELFProgramHeader_p_type |
| 1339 | // |
| 1340 | // Dump an token value for the ELF program header member p_type which |
| 1341 | // describes the type of the program header |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1342 | // ---------------------------------------------------------------------- |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1343 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1344 | ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1345 | { |
| 1346 | const int kStrWidth = 10; |
| 1347 | switch (p_type) |
| 1348 | { |
| 1349 | CASE_AND_STREAM(s, PT_NULL , kStrWidth); |
| 1350 | CASE_AND_STREAM(s, PT_LOAD , kStrWidth); |
| 1351 | CASE_AND_STREAM(s, PT_DYNAMIC , kStrWidth); |
| 1352 | CASE_AND_STREAM(s, PT_INTERP , kStrWidth); |
| 1353 | CASE_AND_STREAM(s, PT_NOTE , kStrWidth); |
| 1354 | CASE_AND_STREAM(s, PT_SHLIB , kStrWidth); |
| 1355 | CASE_AND_STREAM(s, PT_PHDR , kStrWidth); |
| 1356 | default: |
| 1357 | s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, ""); |
| 1358 | break; |
| 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | |
| 1363 | //---------------------------------------------------------------------- |
| 1364 | // DumpELFProgramHeader_p_flags |
| 1365 | // |
| 1366 | // Dump an token value for the ELF program header member p_flags |
| 1367 | //---------------------------------------------------------------------- |
| 1368 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1369 | ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1370 | { |
| 1371 | *s << ((p_flags & PF_X) ? "PF_X" : " ") |
| 1372 | << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ') |
| 1373 | << ((p_flags & PF_W) ? "PF_W" : " ") |
| 1374 | << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ') |
| 1375 | << ((p_flags & PF_R) ? "PF_R" : " "); |
| 1376 | } |
| 1377 | |
| 1378 | //---------------------------------------------------------------------- |
| 1379 | // DumpELFProgramHeaders |
| 1380 | // |
| 1381 | // Dump all of the ELF program header to the specified output stream |
| 1382 | //---------------------------------------------------------------------- |
| 1383 | void |
| 1384 | ObjectFileELF::DumpELFProgramHeaders(Stream *s) |
| 1385 | { |
| 1386 | if (ParseProgramHeaders()) |
| 1387 | { |
| 1388 | s->PutCString("Program Headers\n"); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1389 | s->PutCString("IDX p_type p_offset p_vaddr p_paddr " |
| 1390 | "p_filesz p_memsz p_flags p_align\n"); |
| 1391 | s->PutCString("==== ---------- -------- -------- -------- " |
| 1392 | "-------- -------- ------------------------- --------\n"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1393 | |
| 1394 | uint32_t idx = 0; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1395 | for (ProgramHeaderCollConstIter I = m_program_headers.begin(); |
| 1396 | I != m_program_headers.end(); ++I, ++idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1397 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1398 | s->Printf("[%2u] ", idx); |
| 1399 | ObjectFileELF::DumpELFProgramHeader(s, *I); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1400 | s->EOL(); |
| 1401 | } |
| 1402 | } |
| 1403 | } |
| 1404 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1405 | //---------------------------------------------------------------------- |
| 1406 | // DumpELFSectionHeader |
| 1407 | // |
| 1408 | // Dump a single ELF section header to the specified output stream |
| 1409 | //---------------------------------------------------------------------- |
| 1410 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1411 | ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeader &sh) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1412 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1413 | s->Printf("%8.8x ", sh.sh_name); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1414 | DumpELFSectionHeader_sh_type(s, sh.sh_type); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1415 | s->Printf(" %8.8" PRIx64 " (", sh.sh_flags); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1416 | DumpELFSectionHeader_sh_flags(s, sh.sh_flags); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1417 | 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] | 1418 | s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1419 | 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] | 1420 | } |
| 1421 | |
| 1422 | //---------------------------------------------------------------------- |
| 1423 | // DumpELFSectionHeader_sh_type |
| 1424 | // |
| 1425 | // Dump an token value for the ELF section header member sh_type which |
| 1426 | // describes the type of the section |
| 1427 | //---------------------------------------------------------------------- |
| 1428 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1429 | ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1430 | { |
| 1431 | const int kStrWidth = 12; |
| 1432 | switch (sh_type) |
| 1433 | { |
| 1434 | CASE_AND_STREAM(s, SHT_NULL , kStrWidth); |
| 1435 | CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth); |
| 1436 | CASE_AND_STREAM(s, SHT_SYMTAB , kStrWidth); |
| 1437 | CASE_AND_STREAM(s, SHT_STRTAB , kStrWidth); |
| 1438 | CASE_AND_STREAM(s, SHT_RELA , kStrWidth); |
| 1439 | CASE_AND_STREAM(s, SHT_HASH , kStrWidth); |
| 1440 | CASE_AND_STREAM(s, SHT_DYNAMIC , kStrWidth); |
| 1441 | CASE_AND_STREAM(s, SHT_NOTE , kStrWidth); |
| 1442 | CASE_AND_STREAM(s, SHT_NOBITS , kStrWidth); |
| 1443 | CASE_AND_STREAM(s, SHT_REL , kStrWidth); |
| 1444 | CASE_AND_STREAM(s, SHT_SHLIB , kStrWidth); |
| 1445 | CASE_AND_STREAM(s, SHT_DYNSYM , kStrWidth); |
| 1446 | CASE_AND_STREAM(s, SHT_LOPROC , kStrWidth); |
| 1447 | CASE_AND_STREAM(s, SHT_HIPROC , kStrWidth); |
| 1448 | CASE_AND_STREAM(s, SHT_LOUSER , kStrWidth); |
| 1449 | CASE_AND_STREAM(s, SHT_HIUSER , kStrWidth); |
| 1450 | default: |
| 1451 | s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, ""); |
| 1452 | break; |
| 1453 | } |
| 1454 | } |
| 1455 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1456 | //---------------------------------------------------------------------- |
| 1457 | // DumpELFSectionHeader_sh_flags |
| 1458 | // |
| 1459 | // Dump an token value for the ELF section header member sh_flags |
| 1460 | //---------------------------------------------------------------------- |
| 1461 | void |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 1462 | ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_xword sh_flags) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1463 | { |
| 1464 | *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ") |
| 1465 | << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ') |
| 1466 | << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ") |
| 1467 | << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ') |
| 1468 | << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " "); |
| 1469 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1470 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1471 | //---------------------------------------------------------------------- |
| 1472 | // DumpELFSectionHeaders |
| 1473 | // |
| 1474 | // Dump all of the ELF section header to the specified output stream |
| 1475 | //---------------------------------------------------------------------- |
| 1476 | void |
| 1477 | ObjectFileELF::DumpELFSectionHeaders(Stream *s) |
| 1478 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1479 | if (!(ParseSectionHeaders() && GetSectionHeaderStringTable())) |
| 1480 | return; |
| 1481 | |
| 1482 | s->PutCString("Section Headers\n"); |
| 1483 | s->PutCString("IDX name type flags " |
| 1484 | "addr offset size link info addralgn " |
| 1485 | "entsize Name\n"); |
| 1486 | s->PutCString("==== -------- ------------ -------------------------------- " |
| 1487 | "-------- -------- -------- -------- -------- -------- " |
| 1488 | "-------- ====================\n"); |
| 1489 | |
| 1490 | uint32_t idx = 0; |
| 1491 | for (SectionHeaderCollConstIter I = m_section_headers.begin(); |
| 1492 | I != m_section_headers.end(); ++I, ++idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1493 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1494 | s->Printf("[%2u] ", idx); |
| 1495 | ObjectFileELF::DumpELFSectionHeader(s, *I); |
| 1496 | const char* section_name = m_shstr_data.PeekCStr(I->sh_name); |
| 1497 | if (section_name) |
| 1498 | *s << ' ' << section_name << "\n"; |
| 1499 | } |
| 1500 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1501 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1502 | void |
| 1503 | ObjectFileELF::DumpDependentModules(lldb_private::Stream *s) |
| 1504 | { |
| 1505 | size_t num_modules = ParseDependentModules(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1506 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1507 | if (num_modules > 0) |
| 1508 | { |
| 1509 | s->PutCString("Dependent Modules:\n"); |
| 1510 | for (unsigned i = 0; i < num_modules; ++i) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1511 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1512 | const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i); |
| 1513 | s->Printf(" %s\n", spec.GetFilename().GetCString()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1514 | } |
| 1515 | } |
| 1516 | } |
| 1517 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1518 | bool |
Greg Clayton | 514487e | 2011-02-15 21:59:32 +0000 | [diff] [blame] | 1519 | ObjectFileELF::GetArchitecture (ArchSpec &arch) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1520 | { |
Stephen Wilson | 3f4200fd | 2011-02-24 19:16:15 +0000 | [diff] [blame] | 1521 | if (!ParseHeader()) |
| 1522 | return false; |
| 1523 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 1524 | arch.SetArchitecture (eArchTypeELF, m_header.e_machine, LLDB_INVALID_CPUTYPE); |
Greg Clayton | 64195a2 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 1525 | arch.GetTriple().setOSName (Host::GetOSString().GetCString()); |
| 1526 | arch.GetTriple().setVendorName(Host::GetVendorString().GetCString()); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1527 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Greg Clayton | 9e00b6a65 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 1530 | ObjectFile::Type |
| 1531 | ObjectFileELF::CalculateType() |
| 1532 | { |
| 1533 | switch (m_header.e_type) |
| 1534 | { |
| 1535 | case llvm::ELF::ET_NONE: |
| 1536 | // 0 - No file type |
| 1537 | return eTypeUnknown; |
| 1538 | |
| 1539 | case llvm::ELF::ET_REL: |
| 1540 | // 1 - Relocatable file |
| 1541 | return eTypeObjectFile; |
| 1542 | |
| 1543 | case llvm::ELF::ET_EXEC: |
| 1544 | // 2 - Executable file |
| 1545 | return eTypeExecutable; |
| 1546 | |
| 1547 | case llvm::ELF::ET_DYN: |
| 1548 | // 3 - Shared object file |
| 1549 | return eTypeSharedLibrary; |
| 1550 | |
| 1551 | case ET_CORE: |
| 1552 | // 4 - Core file |
| 1553 | return eTypeCoreFile; |
| 1554 | |
| 1555 | default: |
| 1556 | break; |
| 1557 | } |
| 1558 | return eTypeUnknown; |
| 1559 | } |
| 1560 | |
| 1561 | ObjectFile::Strata |
| 1562 | ObjectFileELF::CalculateStrata() |
| 1563 | { |
| 1564 | switch (m_header.e_type) |
| 1565 | { |
| 1566 | case llvm::ELF::ET_NONE: |
| 1567 | // 0 - No file type |
| 1568 | return eStrataUnknown; |
| 1569 | |
| 1570 | case llvm::ELF::ET_REL: |
| 1571 | // 1 - Relocatable file |
| 1572 | return eStrataUnknown; |
| 1573 | |
| 1574 | case llvm::ELF::ET_EXEC: |
| 1575 | // 2 - Executable file |
| 1576 | // TODO: is there any way to detect that an executable is a kernel |
| 1577 | // related executable by inspecting the program headers, section |
| 1578 | // headers, symbols, or any other flag bits??? |
| 1579 | return eStrataUser; |
| 1580 | |
| 1581 | case llvm::ELF::ET_DYN: |
| 1582 | // 3 - Shared object file |
| 1583 | // TODO: is there any way to detect that an shared library is a kernel |
| 1584 | // related executable by inspecting the program headers, section |
| 1585 | // headers, symbols, or any other flag bits??? |
| 1586 | return eStrataUnknown; |
| 1587 | |
| 1588 | case ET_CORE: |
| 1589 | // 4 - Core file |
| 1590 | // TODO: is there any way to detect that an core file is a kernel |
| 1591 | // related executable by inspecting the program headers, section |
| 1592 | // headers, symbols, or any other flag bits??? |
| 1593 | return eStrataUnknown; |
| 1594 | |
| 1595 | default: |
| 1596 | break; |
| 1597 | } |
| 1598 | return eStrataUnknown; |
| 1599 | } |
| 1600 | |