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