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" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | #include "lldb/Core/PluginManager.h" |
| 20 | #include "lldb/Core/Section.h" |
| 21 | #include "lldb/Core/Stream.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 23 | #define CASE_AND_STREAM(s, def, width) \ |
| 24 | case def: s->Printf("%-*s", width, #def); break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | using namespace lldb; |
| 27 | using namespace lldb_private; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 28 | using namespace elf; |
| 29 | using namespace llvm::ELF; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 31 | //------------------------------------------------------------------ |
| 32 | // Static methods. |
| 33 | //------------------------------------------------------------------ |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | void |
| 35 | ObjectFileELF::Initialize() |
| 36 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 37 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 38 | GetPluginDescriptionStatic(), |
| 39 | CreateInstance); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void |
| 43 | ObjectFileELF::Terminate() |
| 44 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 45 | PluginManager::UnregisterPlugin(CreateInstance); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | const char * |
| 49 | ObjectFileELF::GetPluginNameStatic() |
| 50 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 51 | return "object-file.elf"; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | const char * |
| 55 | ObjectFileELF::GetPluginDescriptionStatic() |
| 56 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 57 | return "ELF object file reader."; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 60 | ObjectFile * |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 61 | ObjectFileELF::CreateInstance(Module *module, |
| 62 | DataBufferSP &data_sp, |
| 63 | const FileSpec *file, addr_t offset, |
| 64 | addr_t length) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 65 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 66 | if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + offset)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 67 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 68 | const uint8_t *magic = data_sp->GetBytes() + offset; |
| 69 | if (ELFHeader::MagicBytesMatch(magic)) |
| 70 | { |
| 71 | unsigned address_size = ELFHeader::AddressSizeInBytes(magic); |
| 72 | if (address_size == 4 || address_size == 8) |
| 73 | { |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 74 | std::auto_ptr<ObjectFileELF> objfile_ap( |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 75 | new ObjectFileELF(module, data_sp, file, offset, length)); |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 76 | ArchSpec spec = objfile_ap->GetArchitecture(); |
| 77 | if (spec.IsValid() && objfile_ap->SetModulesArchitecture(spec)) |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 78 | return objfile_ap.release(); |
| 79 | } |
| 80 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 81 | } |
| 82 | return NULL; |
| 83 | } |
| 84 | |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 85 | ArchSpec |
| 86 | ObjectFileELF::GetArchitecture() |
| 87 | { |
| 88 | if (!ParseHeader()) |
| 89 | return ArchSpec(); |
| 90 | |
| 91 | return ArchSpec(eArchTypeELF, m_header.e_machine, m_header.e_flags); |
| 92 | } |
| 93 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 94 | //------------------------------------------------------------------ |
| 95 | // PluginInterface protocol |
| 96 | //------------------------------------------------------------------ |
| 97 | const char * |
| 98 | ObjectFileELF::GetPluginName() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 99 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 100 | return "ObjectFileELF"; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 103 | const char * |
| 104 | ObjectFileELF::GetShortPluginName() |
| 105 | { |
| 106 | return GetPluginNameStatic(); |
| 107 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 108 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 109 | uint32_t |
| 110 | ObjectFileELF::GetPluginVersion() |
| 111 | { |
| 112 | return m_plugin_version; |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | ObjectFileELF::GetPluginCommandHelp(const char *command, Stream *strm) |
| 117 | { |
| 118 | } |
| 119 | |
| 120 | Error |
| 121 | ObjectFileELF::ExecutePluginCommand(Args &command, Stream *strm) |
| 122 | { |
| 123 | Error error; |
| 124 | error.SetErrorString("No plug-in commands are currently supported."); |
| 125 | return error; |
| 126 | } |
| 127 | |
| 128 | Log * |
| 129 | ObjectFileELF::EnablePluginLogging(Stream *strm, Args &command) |
| 130 | { |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | //------------------------------------------------------------------ |
| 135 | // ObjectFile protocol |
| 136 | //------------------------------------------------------------------ |
| 137 | |
| 138 | ObjectFileELF::ObjectFileELF(Module* module, DataBufferSP& dataSP, |
| 139 | const FileSpec* file, addr_t offset, |
| 140 | addr_t length) |
| 141 | : ObjectFile(module, file, offset, length, dataSP), |
| 142 | m_header(), |
| 143 | m_program_headers(), |
| 144 | m_section_headers(), |
| 145 | m_sections_ap(), |
| 146 | m_symtab_ap(), |
| 147 | m_filespec_ap(), |
| 148 | m_shstr_data() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 149 | { |
| 150 | if (file) |
| 151 | m_file = *file; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 152 | ::memset(&m_header, 0, sizeof(m_header)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 155 | ObjectFileELF::~ObjectFileELF() |
| 156 | { |
| 157 | } |
| 158 | |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 159 | bool |
| 160 | ObjectFileELF::IsExecutable() const |
| 161 | { |
Stephen Wilson | 7f3b57c | 2011-01-15 00:09:50 +0000 | [diff] [blame] | 162 | return m_header.e_entry != 0; |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 165 | Address |
| 166 | ObjectFileELF::GetEntryPoint() const |
| 167 | { |
| 168 | if (m_header.e_entry) |
| 169 | return Address(NULL, m_header.e_entry); |
| 170 | else |
| 171 | return Address(); |
| 172 | } |
| 173 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 174 | ByteOrder |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 175 | ObjectFileELF::GetByteOrder() const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 176 | { |
| 177 | if (m_header.e_ident[EI_DATA] == ELFDATA2MSB) |
| 178 | return eByteOrderBig; |
| 179 | if (m_header.e_ident[EI_DATA] == ELFDATA2LSB) |
| 180 | return eByteOrderLittle; |
| 181 | return eByteOrderInvalid; |
| 182 | } |
| 183 | |
| 184 | size_t |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 185 | ObjectFileELF::GetAddressByteSize() const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 186 | { |
| 187 | return m_data.GetAddressByteSize(); |
| 188 | } |
| 189 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 190 | unsigned |
| 191 | ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 192 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 193 | return std::distance(m_section_headers.begin(), I) + 1; |
| 194 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 195 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 196 | unsigned |
| 197 | ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const |
| 198 | { |
| 199 | return std::distance(m_section_headers.begin(), I) + 1; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | bool |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 203 | ObjectFileELF::ParseHeader() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 204 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 205 | uint32_t offset = GetOffset(); |
| 206 | return m_header.Parse(m_data, &offset); |
| 207 | } |
| 208 | |
| 209 | bool |
Greg Clayton | 6083026 | 2011-02-04 18:53:10 +0000 | [diff] [blame] | 210 | ObjectFileELF::GetUUID(lldb_private::UUID* uuid) |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 211 | { |
| 212 | // FIXME: Return MD5 sum here. See comment in ObjectFile.h. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 213 | return false; |
| 214 | } |
| 215 | |
| 216 | uint32_t |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 217 | ObjectFileELF::GetDependentModules(FileSpecList &files) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 218 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 219 | size_t num_modules = ParseDependentModules(); |
| 220 | uint32_t num_specs = 0; |
| 221 | |
| 222 | for (unsigned i = 0; i < num_modules; ++i) |
| 223 | { |
| 224 | if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i))) |
| 225 | num_specs++; |
| 226 | } |
| 227 | |
| 228 | return num_specs; |
| 229 | } |
| 230 | |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 231 | Address |
| 232 | ObjectFileELF::GetImageInfoAddress() |
| 233 | { |
| 234 | if (!ParseSectionHeaders()) |
| 235 | return Address(); |
| 236 | |
| 237 | user_id_t dynsym_id = 0; |
| 238 | for (SectionHeaderCollIter sh_pos = m_section_headers.begin(); |
| 239 | sh_pos != m_section_headers.end(); ++sh_pos) |
| 240 | { |
| 241 | if (sh_pos->sh_type == SHT_DYNAMIC) |
| 242 | { |
| 243 | dynsym_id = SectionIndex(sh_pos); |
| 244 | break; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | if (!dynsym_id) |
| 249 | return Address(); |
| 250 | |
| 251 | SectionList *section_list = GetSectionList(); |
| 252 | if (!section_list) |
| 253 | return Address(); |
| 254 | |
| 255 | // Resolve the dynamic table entries. |
| 256 | Section *dynsym = section_list->FindSectionByID(dynsym_id).get(); |
| 257 | if (!dynsym) |
| 258 | return Address(); |
| 259 | |
| 260 | DataExtractor dynsym_data; |
| 261 | if (dynsym->ReadSectionDataFromObjectFile(this, dynsym_data)) |
| 262 | { |
| 263 | ELFDynamic symbol; |
| 264 | const unsigned section_size = dynsym_data.GetByteSize(); |
| 265 | unsigned offset = 0; |
| 266 | unsigned cursor = 0; |
| 267 | |
| 268 | // Look for a DT_DEBUG entry. |
| 269 | while (cursor < section_size) |
| 270 | { |
| 271 | offset = cursor; |
| 272 | if (!symbol.Parse(dynsym_data, &cursor)) |
| 273 | break; |
| 274 | |
| 275 | if (symbol.d_tag != DT_DEBUG) |
| 276 | continue; |
| 277 | |
| 278 | return Address(dynsym, offset + sizeof(symbol.d_tag)); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | return Address(); |
| 283 | } |
| 284 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 285 | //---------------------------------------------------------------------- |
| 286 | // ParseDependentModules |
| 287 | //---------------------------------------------------------------------- |
| 288 | size_t |
| 289 | ObjectFileELF::ParseDependentModules() |
| 290 | { |
| 291 | if (m_filespec_ap.get()) |
| 292 | return m_filespec_ap->GetSize(); |
| 293 | |
| 294 | m_filespec_ap.reset(new FileSpecList()); |
| 295 | |
| 296 | if (!(ParseSectionHeaders() && GetSectionHeaderStringTable())) |
| 297 | return 0; |
| 298 | |
| 299 | // Locate the dynamic table. |
| 300 | user_id_t dynsym_id = 0; |
| 301 | user_id_t dynstr_id = 0; |
Greg Clayton | 450e3f3 | 2010-10-12 02:24:53 +0000 | [diff] [blame] | 302 | for (SectionHeaderCollIter sh_pos = m_section_headers.begin(); |
| 303 | sh_pos != m_section_headers.end(); ++sh_pos) |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 304 | { |
Greg Clayton | 450e3f3 | 2010-10-12 02:24:53 +0000 | [diff] [blame] | 305 | if (sh_pos->sh_type == SHT_DYNAMIC) |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 306 | { |
Greg Clayton | 450e3f3 | 2010-10-12 02:24:53 +0000 | [diff] [blame] | 307 | dynsym_id = SectionIndex(sh_pos); |
| 308 | 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] | 309 | break; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | if (!(dynsym_id && dynstr_id)) |
| 314 | return 0; |
| 315 | |
| 316 | SectionList *section_list = GetSectionList(); |
| 317 | if (!section_list) |
| 318 | return 0; |
| 319 | |
| 320 | // Resolve and load the dynamic table entries and corresponding string |
| 321 | // table. |
| 322 | Section *dynsym = section_list->FindSectionByID(dynsym_id).get(); |
| 323 | Section *dynstr = section_list->FindSectionByID(dynstr_id).get(); |
| 324 | if (!(dynsym && dynstr)) |
| 325 | return 0; |
| 326 | |
| 327 | DataExtractor dynsym_data; |
| 328 | DataExtractor dynstr_data; |
| 329 | if (dynsym->ReadSectionDataFromObjectFile(this, dynsym_data) && |
| 330 | dynstr->ReadSectionDataFromObjectFile(this, dynstr_data)) |
| 331 | { |
| 332 | ELFDynamic symbol; |
| 333 | const unsigned section_size = dynsym_data.GetByteSize(); |
| 334 | unsigned offset = 0; |
| 335 | |
| 336 | // The only type of entries we are concerned with are tagged DT_NEEDED, |
| 337 | // yielding the name of a required library. |
| 338 | while (offset < section_size) |
| 339 | { |
| 340 | if (!symbol.Parse(dynsym_data, &offset)) |
| 341 | break; |
| 342 | |
| 343 | if (symbol.d_tag != DT_NEEDED) |
| 344 | continue; |
| 345 | |
| 346 | uint32_t str_index = static_cast<uint32_t>(symbol.d_val); |
| 347 | const char *lib_name = dynstr_data.PeekCStr(str_index); |
Greg Clayton | 274060b | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 348 | m_filespec_ap->Append(FileSpec(lib_name, true)); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
| 352 | return m_filespec_ap->GetSize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | //---------------------------------------------------------------------- |
| 356 | // ParseProgramHeaders |
| 357 | //---------------------------------------------------------------------- |
| 358 | size_t |
| 359 | ObjectFileELF::ParseProgramHeaders() |
| 360 | { |
| 361 | // We have already parsed the program headers |
| 362 | if (!m_program_headers.empty()) |
| 363 | return m_program_headers.size(); |
| 364 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 365 | // If there are no program headers to read we are done. |
| 366 | if (m_header.e_phnum == 0) |
| 367 | return 0; |
| 368 | |
| 369 | m_program_headers.resize(m_header.e_phnum); |
| 370 | if (m_program_headers.size() != m_header.e_phnum) |
| 371 | return 0; |
| 372 | |
| 373 | const size_t ph_size = m_header.e_phnum * m_header.e_phentsize; |
| 374 | const elf_off ph_offset = m_offset + m_header.e_phoff; |
| 375 | DataBufferSP buffer_sp(m_file.ReadFileContents(ph_offset, ph_size)); |
| 376 | |
| 377 | if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != ph_size) |
| 378 | return 0; |
| 379 | |
| 380 | DataExtractor data(buffer_sp, m_data.GetByteOrder(), |
| 381 | m_data.GetAddressByteSize()); |
| 382 | |
| 383 | uint32_t idx; |
| 384 | uint32_t offset; |
| 385 | for (idx = 0, offset = 0; idx < m_header.e_phnum; ++idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 386 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 387 | if (m_program_headers[idx].Parse(data, &offset) == false) |
| 388 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 391 | if (idx < m_program_headers.size()) |
| 392 | m_program_headers.resize(idx); |
| 393 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 394 | return m_program_headers.size(); |
| 395 | } |
| 396 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 397 | //---------------------------------------------------------------------- |
| 398 | // ParseSectionHeaders |
| 399 | //---------------------------------------------------------------------- |
| 400 | size_t |
| 401 | ObjectFileELF::ParseSectionHeaders() |
| 402 | { |
| 403 | // We have already parsed the section headers |
| 404 | if (!m_section_headers.empty()) |
| 405 | return m_section_headers.size(); |
| 406 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 407 | // If there are no section headers we are done. |
| 408 | if (m_header.e_shnum == 0) |
| 409 | return 0; |
| 410 | |
| 411 | m_section_headers.resize(m_header.e_shnum); |
| 412 | if (m_section_headers.size() != m_header.e_shnum) |
| 413 | return 0; |
| 414 | |
| 415 | const size_t sh_size = m_header.e_shnum * m_header.e_shentsize; |
| 416 | const elf_off sh_offset = m_offset + m_header.e_shoff; |
| 417 | DataBufferSP buffer_sp(m_file.ReadFileContents(sh_offset, sh_size)); |
| 418 | |
| 419 | if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != sh_size) |
| 420 | return 0; |
| 421 | |
| 422 | DataExtractor data(buffer_sp, |
| 423 | m_data.GetByteOrder(), |
| 424 | m_data.GetAddressByteSize()); |
| 425 | |
| 426 | uint32_t idx; |
| 427 | uint32_t offset; |
| 428 | for (idx = 0, offset = 0; idx < m_header.e_shnum; ++idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 429 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 430 | if (m_section_headers[idx].Parse(data, &offset) == false) |
| 431 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 432 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 433 | if (idx < m_section_headers.size()) |
| 434 | m_section_headers.resize(idx); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 435 | |
| 436 | return m_section_headers.size(); |
| 437 | } |
| 438 | |
| 439 | size_t |
| 440 | ObjectFileELF::GetSectionHeaderStringTable() |
| 441 | { |
| 442 | if (m_shstr_data.GetByteSize() == 0) |
| 443 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 444 | const unsigned strtab_idx = m_header.e_shstrndx; |
| 445 | |
| 446 | if (strtab_idx && strtab_idx < m_section_headers.size()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 447 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 448 | const ELFSectionHeader &sheader = m_section_headers[strtab_idx]; |
| 449 | const size_t byte_size = sheader.sh_size; |
| 450 | const Elf64_Off offset = m_offset + sheader.sh_offset; |
| 451 | DataBufferSP buffer_sp(m_file.ReadFileContents(offset, byte_size)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 452 | |
| 453 | if (buffer_sp.get() == NULL || buffer_sp->GetByteSize() != byte_size) |
| 454 | return 0; |
| 455 | |
| 456 | m_shstr_data.SetData(buffer_sp); |
| 457 | } |
| 458 | } |
| 459 | return m_shstr_data.GetByteSize(); |
| 460 | } |
| 461 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 462 | lldb::user_id_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 463 | ObjectFileELF::GetSectionIndexByName(const char *name) |
| 464 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 465 | if (!(ParseSectionHeaders() && GetSectionHeaderStringTable())) |
| 466 | return 0; |
| 467 | |
| 468 | // Search the collection of section headers for one with a matching name. |
| 469 | for (SectionHeaderCollIter I = m_section_headers.begin(); |
| 470 | I != m_section_headers.end(); ++I) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 471 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 472 | const char *sectionName = m_shstr_data.PeekCStr(I->sh_name); |
| 473 | |
| 474 | if (!sectionName) |
| 475 | return 0; |
| 476 | |
| 477 | if (strcmp(name, sectionName) != 0) |
| 478 | continue; |
| 479 | |
| 480 | return SectionIndex(I); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 483 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | SectionList * |
| 487 | ObjectFileELF::GetSectionList() |
| 488 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 489 | if (m_sections_ap.get()) |
| 490 | return m_sections_ap.get(); |
| 491 | |
| 492 | if (ParseSectionHeaders() && GetSectionHeaderStringTable()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 493 | { |
| 494 | m_sections_ap.reset(new SectionList()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 495 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 496 | for (SectionHeaderCollIter I = m_section_headers.begin(); |
| 497 | I != m_section_headers.end(); ++I) |
| 498 | { |
| 499 | const ELFSectionHeader &header = *I; |
| 500 | |
| 501 | ConstString name(m_shstr_data.PeekCStr(header.sh_name)); |
| 502 | uint64_t size = header.sh_type == SHT_NOBITS ? 0 : header.sh_size; |
| 503 | |
Greg Clayton | 4ceb998 | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 504 | static ConstString g_sect_name_text (".text"); |
| 505 | static ConstString g_sect_name_data (".data"); |
| 506 | static ConstString g_sect_name_bss (".bss"); |
| 507 | static ConstString g_sect_name_dwarf_debug_abbrev (".debug_abbrev"); |
| 508 | static ConstString g_sect_name_dwarf_debug_aranges (".debug_aranges"); |
| 509 | static ConstString g_sect_name_dwarf_debug_frame (".debug_frame"); |
| 510 | static ConstString g_sect_name_dwarf_debug_info (".debug_info"); |
| 511 | static ConstString g_sect_name_dwarf_debug_line (".debug_line"); |
| 512 | static ConstString g_sect_name_dwarf_debug_loc (".debug_loc"); |
| 513 | static ConstString g_sect_name_dwarf_debug_macinfo (".debug_macinfo"); |
| 514 | static ConstString g_sect_name_dwarf_debug_pubnames (".debug_pubnames"); |
| 515 | static ConstString g_sect_name_dwarf_debug_pubtypes (".debug_pubtypes"); |
| 516 | static ConstString g_sect_name_dwarf_debug_ranges (".debug_ranges"); |
| 517 | static ConstString g_sect_name_dwarf_debug_str (".debug_str"); |
| 518 | static ConstString g_sect_name_eh_frame (".eh_frame"); |
| 519 | |
| 520 | SectionType sect_type = eSectionTypeOther; |
| 521 | |
| 522 | if (name == g_sect_name_text) sect_type = eSectionTypeCode; |
| 523 | else if (name == g_sect_name_data) sect_type = eSectionTypeData; |
| 524 | else if (name == g_sect_name_bss) sect_type = eSectionTypeZeroFill; |
| 525 | else if (name == g_sect_name_dwarf_debug_abbrev) sect_type = eSectionTypeDWARFDebugAbbrev; |
| 526 | else if (name == g_sect_name_dwarf_debug_aranges) sect_type = eSectionTypeDWARFDebugAranges; |
| 527 | else if (name == g_sect_name_dwarf_debug_frame) sect_type = eSectionTypeDWARFDebugFrame; |
| 528 | else if (name == g_sect_name_dwarf_debug_info) sect_type = eSectionTypeDWARFDebugInfo; |
| 529 | else if (name == g_sect_name_dwarf_debug_line) sect_type = eSectionTypeDWARFDebugLine; |
| 530 | else if (name == g_sect_name_dwarf_debug_loc) sect_type = eSectionTypeDWARFDebugLoc; |
| 531 | else if (name == g_sect_name_dwarf_debug_macinfo) sect_type = eSectionTypeDWARFDebugMacInfo; |
| 532 | else if (name == g_sect_name_dwarf_debug_pubnames) sect_type = eSectionTypeDWARFDebugPubNames; |
| 533 | else if (name == g_sect_name_dwarf_debug_pubtypes) sect_type = eSectionTypeDWARFDebugPubTypes; |
| 534 | else if (name == g_sect_name_dwarf_debug_ranges) sect_type = eSectionTypeDWARFDebugRanges; |
| 535 | else if (name == g_sect_name_dwarf_debug_str) sect_type = eSectionTypeDWARFDebugStr; |
| 536 | else if (name == g_sect_name_eh_frame) sect_type = eSectionTypeEHFrame; |
| 537 | |
| 538 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 539 | SectionSP section(new Section( |
Greg Clayton | 4ceb998 | 2010-07-21 22:54:26 +0000 | [diff] [blame] | 540 | 0, // Parent section. |
| 541 | GetModule(), // Module to which this section belongs. |
| 542 | SectionIndex(I), // Section ID. |
| 543 | name, // Section name. |
| 544 | sect_type, // Section type. |
| 545 | header.sh_addr, // VM address. |
| 546 | header.sh_size, // VM size in bytes of this section. |
| 547 | header.sh_offset, // Offset of this section in the file. |
| 548 | size, // Size of the section as found in the file. |
| 549 | header.sh_flags)); // Flags for this section. |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 550 | |
| 551 | m_sections_ap->AddSection(section); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 552 | } |
| 553 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 554 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 555 | return m_sections_ap.get(); |
| 556 | } |
| 557 | |
| 558 | static void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 559 | ParseSymbols(Symtab *symtab, SectionList *section_list, |
| 560 | const ELFSectionHeader &symtab_shdr, |
| 561 | const DataExtractor &symtab_data, |
| 562 | const DataExtractor &strtab_data) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 563 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 564 | ELFSymbol symbol; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 565 | uint32_t offset = 0; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 566 | const unsigned numSymbols = |
| 567 | symtab_data.GetByteSize() / symtab_shdr.sh_entsize; |
| 568 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 569 | static ConstString text_section_name(".text"); |
| 570 | static ConstString init_section_name(".init"); |
| 571 | static ConstString fini_section_name(".fini"); |
| 572 | static ConstString ctors_section_name(".ctors"); |
| 573 | static ConstString dtors_section_name(".dtors"); |
| 574 | |
| 575 | static ConstString data_section_name(".data"); |
| 576 | static ConstString rodata_section_name(".rodata"); |
| 577 | static ConstString rodata1_section_name(".rodata1"); |
| 578 | static ConstString data2_section_name(".data1"); |
| 579 | static ConstString bss_section_name(".bss"); |
| 580 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 581 | for (unsigned i = 0; i < numSymbols; ++i) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 582 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 583 | if (symbol.Parse(symtab_data, &offset) == false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 584 | break; |
| 585 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 586 | Section *symbol_section = NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 587 | SymbolType symbol_type = eSymbolTypeInvalid; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 588 | Elf64_Half symbol_idx = symbol.st_shndx; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 589 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 590 | switch (symbol_idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 591 | { |
| 592 | case SHN_ABS: |
| 593 | symbol_type = eSymbolTypeAbsolute; |
| 594 | break; |
| 595 | case SHN_UNDEF: |
| 596 | symbol_type = eSymbolTypeUndefined; |
| 597 | break; |
| 598 | default: |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 599 | symbol_section = section_list->GetSectionAtIndex(symbol_idx).get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 600 | break; |
| 601 | } |
| 602 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 603 | switch (symbol.getType()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 604 | { |
| 605 | default: |
| 606 | case STT_NOTYPE: |
| 607 | // The symbol's type is not specified. |
| 608 | break; |
| 609 | |
| 610 | case STT_OBJECT: |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 611 | // The symbol is associated with a data object, such as a variable, |
| 612 | // an array, etc. |
| 613 | symbol_type = eSymbolTypeData; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 614 | break; |
| 615 | |
| 616 | case STT_FUNC: |
| 617 | // The symbol is associated with a function or other executable code. |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 618 | symbol_type = eSymbolTypeCode; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 619 | break; |
| 620 | |
| 621 | case STT_SECTION: |
| 622 | // The symbol is associated with a section. Symbol table entries of |
| 623 | // this type exist primarily for relocation and normally have |
| 624 | // STB_LOCAL binding. |
| 625 | break; |
| 626 | |
| 627 | case STT_FILE: |
| 628 | // Conventionally, the symbol's name gives the name of the source |
| 629 | // file associated with the object file. A file symbol has STB_LOCAL |
| 630 | // binding, its section index is SHN_ABS, and it precedes the other |
| 631 | // STB_LOCAL symbols for the file, if it is present. |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 632 | symbol_type = eSymbolTypeObjectFile; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 633 | break; |
| 634 | } |
| 635 | |
| 636 | if (symbol_type == eSymbolTypeInvalid) |
| 637 | { |
| 638 | if (symbol_section) |
| 639 | { |
| 640 | const ConstString §_name = symbol_section->GetName(); |
| 641 | if (sect_name == text_section_name || |
| 642 | sect_name == init_section_name || |
| 643 | sect_name == fini_section_name || |
| 644 | sect_name == ctors_section_name || |
| 645 | sect_name == dtors_section_name) |
| 646 | { |
| 647 | symbol_type = eSymbolTypeCode; |
| 648 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 649 | else if (sect_name == data_section_name || |
| 650 | sect_name == data2_section_name || |
| 651 | sect_name == rodata_section_name || |
| 652 | sect_name == rodata1_section_name || |
| 653 | sect_name == bss_section_name) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 654 | { |
| 655 | symbol_type = eSymbolTypeData; |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | uint64_t symbol_value = symbol.st_value; |
| 661 | if (symbol_section != NULL) |
| 662 | symbol_value -= symbol_section->GetFileAddress(); |
| 663 | const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 664 | bool is_global = symbol.getBinding() == STB_GLOBAL; |
| 665 | uint32_t flags = symbol.st_other << 8 | symbol.st_info; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 666 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 667 | Symbol dc_symbol( |
| 668 | i, // ID is the original symbol table index. |
| 669 | symbol_name, // Symbol name. |
| 670 | false, // Is the symbol name mangled? |
| 671 | symbol_type, // Type of this symbol |
| 672 | is_global, // Is this globally visible? |
| 673 | false, // Is this symbol debug info? |
| 674 | false, // Is this symbol a trampoline? |
| 675 | false, // Is this symbol artificial? |
| 676 | symbol_section, // Section in which this symbol is defined or null. |
| 677 | symbol_value, // Offset in section or symbol value. |
| 678 | symbol.st_size, // Size in bytes of this symbol. |
| 679 | flags); // Symbol flags. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 680 | symtab->AddSymbol(dc_symbol); |
| 681 | } |
| 682 | } |
| 683 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 684 | void |
| 685 | ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, |
| 686 | const ELFSectionHeader &symtab_hdr, |
| 687 | user_id_t symtab_id) |
| 688 | { |
| 689 | assert(symtab_hdr.sh_type == SHT_SYMTAB || |
| 690 | symtab_hdr.sh_type == SHT_DYNSYM); |
| 691 | |
| 692 | // Parse in the section list if needed. |
| 693 | SectionList *section_list = GetSectionList(); |
| 694 | if (!section_list) |
| 695 | return; |
| 696 | |
| 697 | // Section ID's are ones based. |
| 698 | user_id_t strtab_id = symtab_hdr.sh_link + 1; |
| 699 | |
| 700 | Section *symtab = section_list->FindSectionByID(symtab_id).get(); |
| 701 | Section *strtab = section_list->FindSectionByID(strtab_id).get(); |
| 702 | if (symtab && strtab) |
| 703 | { |
| 704 | DataExtractor symtab_data; |
| 705 | DataExtractor strtab_data; |
| 706 | if (symtab->ReadSectionDataFromObjectFile(this, symtab_data) && |
| 707 | strtab->ReadSectionDataFromObjectFile(this, strtab_data)) |
| 708 | { |
| 709 | ParseSymbols(symbol_table, section_list, symtab_hdr, |
| 710 | symtab_data, strtab_data); |
| 711 | } |
| 712 | } |
| 713 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 714 | |
| 715 | Symtab * |
| 716 | ObjectFileELF::GetSymtab() |
| 717 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 718 | if (m_symtab_ap.get()) |
| 719 | return m_symtab_ap.get(); |
| 720 | |
| 721 | Symtab *symbol_table = new Symtab(this); |
| 722 | m_symtab_ap.reset(symbol_table); |
| 723 | |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 724 | Mutex::Locker locker (symbol_table->GetMutex ()); |
| 725 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 726 | if (!(ParseSectionHeaders() && GetSectionHeaderStringTable())) |
| 727 | return symbol_table; |
| 728 | |
| 729 | // Locate and parse all linker symbol tables. |
| 730 | for (SectionHeaderCollIter I = m_section_headers.begin(); |
| 731 | I != m_section_headers.end(); ++I) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 732 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 733 | if (I->sh_type == SHT_SYMTAB) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 734 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 735 | const ELFSectionHeader &symtab_section = *I; |
| 736 | user_id_t section_id = SectionIndex(I); |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 737 | ParseSymbolTable (symbol_table, symtab_section, section_id); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 738 | } |
| 739 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 740 | |
| 741 | return symbol_table; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 744 | //===----------------------------------------------------------------------===// |
| 745 | // Dump |
| 746 | // |
| 747 | // Dump the specifics of the runtime file container (such as any headers |
| 748 | // segments, sections, etc). |
| 749 | //---------------------------------------------------------------------- |
| 750 | void |
| 751 | ObjectFileELF::Dump(Stream *s) |
| 752 | { |
| 753 | DumpELFHeader(s, m_header); |
| 754 | s->EOL(); |
| 755 | DumpELFProgramHeaders(s); |
| 756 | s->EOL(); |
| 757 | DumpELFSectionHeaders(s); |
| 758 | s->EOL(); |
| 759 | SectionList *section_list = GetSectionList(); |
| 760 | if (section_list) |
Greg Clayton | 10177aa | 2010-12-08 05:08:21 +0000 | [diff] [blame] | 761 | section_list->Dump(s, NULL, true, UINT32_MAX); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 762 | Symtab *symtab = GetSymtab(); |
| 763 | if (symtab) |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 764 | symtab->Dump(s, NULL, lldb::eSortOrderNone); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 765 | s->EOL(); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 766 | DumpDependentModules(s); |
| 767 | s->EOL(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | //---------------------------------------------------------------------- |
| 771 | // DumpELFHeader |
| 772 | // |
| 773 | // Dump the ELF header to the specified output stream |
| 774 | //---------------------------------------------------------------------- |
| 775 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 776 | ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 777 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 778 | s->PutCString("ELF Header\n"); |
| 779 | s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]); |
| 780 | s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n", |
| 781 | header.e_ident[EI_MAG1], header.e_ident[EI_MAG1]); |
| 782 | s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n", |
| 783 | header.e_ident[EI_MAG2], header.e_ident[EI_MAG2]); |
| 784 | s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n", |
| 785 | header.e_ident[EI_MAG3], header.e_ident[EI_MAG3]); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 786 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 787 | s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]); |
| 788 | 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] | 789 | DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]); |
| 790 | s->Printf ("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]); |
| 791 | s->Printf ("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]); |
| 792 | |
| 793 | s->Printf("e_type = 0x%4.4x ", header.e_type); |
| 794 | DumpELFHeader_e_type(s, header.e_type); |
| 795 | s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine); |
| 796 | s->Printf("e_version = 0x%8.8x\n", header.e_version); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 797 | s->Printf("e_entry = 0x%8.8lx\n", header.e_entry); |
| 798 | s->Printf("e_phoff = 0x%8.8lx\n", header.e_phoff); |
| 799 | s->Printf("e_shoff = 0x%8.8lx\n", header.e_shoff); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 800 | s->Printf("e_flags = 0x%8.8x\n", header.e_flags); |
| 801 | s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize); |
| 802 | s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize); |
| 803 | s->Printf("e_phnum = 0x%4.4x\n", header.e_phnum); |
| 804 | s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize); |
| 805 | s->Printf("e_shnum = 0x%4.4x\n", header.e_shnum); |
| 806 | s->Printf("e_shstrndx = 0x%4.4x\n", header.e_shstrndx); |
| 807 | } |
| 808 | |
| 809 | //---------------------------------------------------------------------- |
| 810 | // DumpELFHeader_e_type |
| 811 | // |
| 812 | // Dump an token value for the ELF header member e_type |
| 813 | //---------------------------------------------------------------------- |
| 814 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 815 | ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 816 | { |
| 817 | switch (e_type) |
| 818 | { |
| 819 | case ET_NONE: *s << "ET_NONE"; break; |
| 820 | case ET_REL: *s << "ET_REL"; break; |
| 821 | case ET_EXEC: *s << "ET_EXEC"; break; |
| 822 | case ET_DYN: *s << "ET_DYN"; break; |
| 823 | case ET_CORE: *s << "ET_CORE"; break; |
| 824 | default: |
| 825 | break; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | //---------------------------------------------------------------------- |
| 830 | // DumpELFHeader_e_ident_EI_DATA |
| 831 | // |
| 832 | // Dump an token value for the ELF header member e_ident[EI_DATA] |
| 833 | //---------------------------------------------------------------------- |
| 834 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 835 | ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, unsigned char ei_data) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 836 | { |
| 837 | switch (ei_data) |
| 838 | { |
| 839 | case ELFDATANONE: *s << "ELFDATANONE"; break; |
| 840 | case ELFDATA2LSB: *s << "ELFDATA2LSB - Little Endian"; break; |
| 841 | case ELFDATA2MSB: *s << "ELFDATA2MSB - Big Endian"; break; |
| 842 | default: |
| 843 | break; |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | |
| 848 | //---------------------------------------------------------------------- |
| 849 | // DumpELFProgramHeader |
| 850 | // |
| 851 | // Dump a single ELF program header to the specified output stream |
| 852 | //---------------------------------------------------------------------- |
| 853 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 854 | ObjectFileELF::DumpELFProgramHeader(Stream *s, const ELFProgramHeader &ph) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 855 | { |
| 856 | DumpELFProgramHeader_p_type(s, ph.p_type); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 857 | s->Printf(" %8.8lx %8.8lx %8.8lx", ph.p_offset, ph.p_vaddr, ph.p_paddr); |
| 858 | s->Printf(" %8.8lx %8.8lx %8.8lx (", ph.p_filesz, ph.p_memsz, ph.p_flags); |
| 859 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 860 | DumpELFProgramHeader_p_flags(s, ph.p_flags); |
| 861 | s->Printf(") %8.8x", ph.p_align); |
| 862 | } |
| 863 | |
| 864 | //---------------------------------------------------------------------- |
| 865 | // DumpELFProgramHeader_p_type |
| 866 | // |
| 867 | // Dump an token value for the ELF program header member p_type which |
| 868 | // describes the type of the program header |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 869 | // ---------------------------------------------------------------------- |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 870 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 871 | ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 872 | { |
| 873 | const int kStrWidth = 10; |
| 874 | switch (p_type) |
| 875 | { |
| 876 | CASE_AND_STREAM(s, PT_NULL , kStrWidth); |
| 877 | CASE_AND_STREAM(s, PT_LOAD , kStrWidth); |
| 878 | CASE_AND_STREAM(s, PT_DYNAMIC , kStrWidth); |
| 879 | CASE_AND_STREAM(s, PT_INTERP , kStrWidth); |
| 880 | CASE_AND_STREAM(s, PT_NOTE , kStrWidth); |
| 881 | CASE_AND_STREAM(s, PT_SHLIB , kStrWidth); |
| 882 | CASE_AND_STREAM(s, PT_PHDR , kStrWidth); |
| 883 | default: |
| 884 | s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, ""); |
| 885 | break; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | |
| 890 | //---------------------------------------------------------------------- |
| 891 | // DumpELFProgramHeader_p_flags |
| 892 | // |
| 893 | // Dump an token value for the ELF program header member p_flags |
| 894 | //---------------------------------------------------------------------- |
| 895 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 896 | ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 897 | { |
| 898 | *s << ((p_flags & PF_X) ? "PF_X" : " ") |
| 899 | << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ') |
| 900 | << ((p_flags & PF_W) ? "PF_W" : " ") |
| 901 | << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ') |
| 902 | << ((p_flags & PF_R) ? "PF_R" : " "); |
| 903 | } |
| 904 | |
| 905 | //---------------------------------------------------------------------- |
| 906 | // DumpELFProgramHeaders |
| 907 | // |
| 908 | // Dump all of the ELF program header to the specified output stream |
| 909 | //---------------------------------------------------------------------- |
| 910 | void |
| 911 | ObjectFileELF::DumpELFProgramHeaders(Stream *s) |
| 912 | { |
| 913 | if (ParseProgramHeaders()) |
| 914 | { |
| 915 | s->PutCString("Program Headers\n"); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 916 | s->PutCString("IDX p_type p_offset p_vaddr p_paddr " |
| 917 | "p_filesz p_memsz p_flags p_align\n"); |
| 918 | s->PutCString("==== ---------- -------- -------- -------- " |
| 919 | "-------- -------- ------------------------- --------\n"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 920 | |
| 921 | uint32_t idx = 0; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 922 | for (ProgramHeaderCollConstIter I = m_program_headers.begin(); |
| 923 | I != m_program_headers.end(); ++I, ++idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 924 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 925 | s->Printf("[%2u] ", idx); |
| 926 | ObjectFileELF::DumpELFProgramHeader(s, *I); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 927 | s->EOL(); |
| 928 | } |
| 929 | } |
| 930 | } |
| 931 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 932 | //---------------------------------------------------------------------- |
| 933 | // DumpELFSectionHeader |
| 934 | // |
| 935 | // Dump a single ELF section header to the specified output stream |
| 936 | //---------------------------------------------------------------------- |
| 937 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 938 | ObjectFileELF::DumpELFSectionHeader(Stream *s, const ELFSectionHeader &sh) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 939 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 940 | s->Printf("%8.8x ", sh.sh_name); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 941 | DumpELFSectionHeader_sh_type(s, sh.sh_type); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 942 | s->Printf(" %8.8lx (", sh.sh_flags); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 943 | DumpELFSectionHeader_sh_flags(s, sh.sh_flags); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 944 | s->Printf(") %8.8lx %8.8lx %8.8lx", sh.sh_addr, sh.sh_offset, sh.sh_size); |
| 945 | s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info); |
| 946 | s->Printf(" %8.8lx %8.8lx", sh.sh_addralign, sh.sh_entsize); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | //---------------------------------------------------------------------- |
| 950 | // DumpELFSectionHeader_sh_type |
| 951 | // |
| 952 | // Dump an token value for the ELF section header member sh_type which |
| 953 | // describes the type of the section |
| 954 | //---------------------------------------------------------------------- |
| 955 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 956 | ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 957 | { |
| 958 | const int kStrWidth = 12; |
| 959 | switch (sh_type) |
| 960 | { |
| 961 | CASE_AND_STREAM(s, SHT_NULL , kStrWidth); |
| 962 | CASE_AND_STREAM(s, SHT_PROGBITS , kStrWidth); |
| 963 | CASE_AND_STREAM(s, SHT_SYMTAB , kStrWidth); |
| 964 | CASE_AND_STREAM(s, SHT_STRTAB , kStrWidth); |
| 965 | CASE_AND_STREAM(s, SHT_RELA , kStrWidth); |
| 966 | CASE_AND_STREAM(s, SHT_HASH , kStrWidth); |
| 967 | CASE_AND_STREAM(s, SHT_DYNAMIC , kStrWidth); |
| 968 | CASE_AND_STREAM(s, SHT_NOTE , kStrWidth); |
| 969 | CASE_AND_STREAM(s, SHT_NOBITS , kStrWidth); |
| 970 | CASE_AND_STREAM(s, SHT_REL , kStrWidth); |
| 971 | CASE_AND_STREAM(s, SHT_SHLIB , kStrWidth); |
| 972 | CASE_AND_STREAM(s, SHT_DYNSYM , kStrWidth); |
| 973 | CASE_AND_STREAM(s, SHT_LOPROC , kStrWidth); |
| 974 | CASE_AND_STREAM(s, SHT_HIPROC , kStrWidth); |
| 975 | CASE_AND_STREAM(s, SHT_LOUSER , kStrWidth); |
| 976 | CASE_AND_STREAM(s, SHT_HIUSER , kStrWidth); |
| 977 | default: |
| 978 | s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, ""); |
| 979 | break; |
| 980 | } |
| 981 | } |
| 982 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 983 | //---------------------------------------------------------------------- |
| 984 | // DumpELFSectionHeader_sh_flags |
| 985 | // |
| 986 | // Dump an token value for the ELF section header member sh_flags |
| 987 | //---------------------------------------------------------------------- |
| 988 | void |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 989 | ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, elf_word sh_flags) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 990 | { |
| 991 | *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ") |
| 992 | << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ') |
| 993 | << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ") |
| 994 | << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ') |
| 995 | << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " "); |
| 996 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 997 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 998 | //---------------------------------------------------------------------- |
| 999 | // DumpELFSectionHeaders |
| 1000 | // |
| 1001 | // Dump all of the ELF section header to the specified output stream |
| 1002 | //---------------------------------------------------------------------- |
| 1003 | void |
| 1004 | ObjectFileELF::DumpELFSectionHeaders(Stream *s) |
| 1005 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1006 | if (!(ParseSectionHeaders() && GetSectionHeaderStringTable())) |
| 1007 | return; |
| 1008 | |
| 1009 | s->PutCString("Section Headers\n"); |
| 1010 | s->PutCString("IDX name type flags " |
| 1011 | "addr offset size link info addralgn " |
| 1012 | "entsize Name\n"); |
| 1013 | s->PutCString("==== -------- ------------ -------------------------------- " |
| 1014 | "-------- -------- -------- -------- -------- -------- " |
| 1015 | "-------- ====================\n"); |
| 1016 | |
| 1017 | uint32_t idx = 0; |
| 1018 | for (SectionHeaderCollConstIter I = m_section_headers.begin(); |
| 1019 | I != m_section_headers.end(); ++I, ++idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1020 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1021 | s->Printf("[%2u] ", idx); |
| 1022 | ObjectFileELF::DumpELFSectionHeader(s, *I); |
| 1023 | const char* section_name = m_shstr_data.PeekCStr(I->sh_name); |
| 1024 | if (section_name) |
| 1025 | *s << ' ' << section_name << "\n"; |
| 1026 | } |
| 1027 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1028 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1029 | void |
| 1030 | ObjectFileELF::DumpDependentModules(lldb_private::Stream *s) |
| 1031 | { |
| 1032 | size_t num_modules = ParseDependentModules(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1033 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1034 | if (num_modules > 0) |
| 1035 | { |
| 1036 | s->PutCString("Dependent Modules:\n"); |
| 1037 | for (unsigned i = 0; i < num_modules; ++i) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1038 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1039 | const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i); |
| 1040 | s->Printf(" %s\n", spec.GetFilename().GetCString()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1045 | bool |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1046 | ObjectFileELF::GetTargetTriple(ConstString &target_triple) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1047 | { |
| 1048 | static ConstString g_target_triple; |
| 1049 | |
| 1050 | if (g_target_triple) |
| 1051 | { |
| 1052 | target_triple = g_target_triple; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1053 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1054 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1055 | |
| 1056 | std::string triple; |
| 1057 | switch (m_header.e_machine) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1058 | { |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1059 | default: |
| 1060 | assert(false && "Unexpected machine type."); |
| 1061 | break; |
| 1062 | case EM_SPARC: triple.assign("sparc-"); break; |
| 1063 | case EM_386: triple.assign("i386-"); break; |
| 1064 | case EM_68K: triple.assign("68k-"); break; |
| 1065 | case EM_88K: triple.assign("88k-"); break; |
| 1066 | case EM_860: triple.assign("i860-"); break; |
| 1067 | case EM_MIPS: triple.assign("mips-"); break; |
| 1068 | case EM_PPC: triple.assign("powerpc-"); break; |
| 1069 | case EM_PPC64: triple.assign("powerpc64-"); break; |
| 1070 | case EM_ARM: triple.assign("arm-"); break; |
| 1071 | case EM_X86_64: triple.assign("x86_64-"); break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1072 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1073 | // TODO: determine if there is a vendor in the ELF? Default to "linux" for now |
| 1074 | triple += "linux-"; |
| 1075 | // TODO: determine if there is an OS in the ELF? Default to "gnu" for now |
| 1076 | triple += "gnu"; |
| 1077 | g_target_triple.SetCString(triple.c_str()); |
| 1078 | target_triple = g_target_triple; |
| 1079 | |
| 1080 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |