blob: 1dbea48a2b2323742be13ee30fca322e45d00a15 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- ObjectFile.cpp ------------------------------------------*- C++ -*-===//
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 "lldb/lldb-private.h"
11#include "lldb/Core/Module.h"
12#include "lldb/Core/PluginManager.h"
13#include "lldb/Core/RegularExpression.h"
14#include "lldb/Core/Timer.h"
15#include "lldb/Symbol/ObjectFile.h"
16#include "lldb/Symbol/ObjectContainer.h"
17#include "lldb/Symbol/SymbolFile.h"
18
19using namespace lldb;
20using namespace lldb_private;
21
22ObjectFile*
23ObjectFile::FindPlugin (Module* module, const FileSpec* file, lldb::addr_t file_offset, lldb::addr_t file_size)
24{
25 Timer scoped_timer (__PRETTY_FUNCTION__,
26 "ObjectFile::FindPlugin (module = %s/%s, file = %p, file_offset = 0x%z8.8x, file_size = 0x%z8.8x)",
27 module->GetFileSpec().GetDirectory().AsCString(),
28 module->GetFileSpec().GetFilename().AsCString(),
29 file, file_offset, file_size);
30 std::auto_ptr<ObjectFile> object_file_ap;
31
32 if (module != NULL)
33 {
34 if (file)
35 {
36 if (file_size == 0)
37 file_size = file->GetByteSize();
38
39 if (file_size == 0)
40 {
41 // Check for archive file with format "/path/to/archive.a(object.o)"
42 char path_with_object[PATH_MAX*2];
43 module->GetFileSpec().GetPath(path_with_object, sizeof(path_with_object));
44
45 RegularExpression g_object_regex("(.*)\\(([^\\)]+)\\)$");
46 if (g_object_regex.Execute (path_with_object, 2))
47 {
48 FileSpec archive_file;
49 std::string path;
50 std::string object;
51 if (g_object_regex.GetMatchAtIndex (path_with_object, 1, path) &&
52 g_object_regex.GetMatchAtIndex (path_with_object, 2, object))
53 {
Greg Clayton537a7a82010-10-20 20:54:39 +000054 archive_file.SetFile (path.c_str(), false);
Chris Lattner24943d22010-06-08 16:52:24 +000055 file_size = archive_file.GetByteSize();
56 if (file_size > 0)
57 module->SetFileSpecAndObjectName (archive_file, ConstString(object.c_str()));
58 }
59 }
60 }
61
Johnny Chen89e4bae2010-11-05 21:43:19 +000062 // No need to delegate further if (file_offset, file_size) exceeds the total file size.
63 // This is the base case.
64 if (file_offset + file_size > file->GetByteSize())
65 return NULL;
66
Chris Lattner24943d22010-06-08 16:52:24 +000067 DataBufferSP file_header_data_sp(file->ReadFileContents(file_offset, 512));
68 uint32_t idx;
69
70 // Check if this is a normal object file by iterating through
71 // all object file plugin instances.
72 ObjectFileCreateInstance create_object_file_callback;
73 for (idx = 0; (create_object_file_callback = PluginManager::GetObjectFileCreateCallbackAtIndex(idx)) != NULL; ++idx)
74 {
75 object_file_ap.reset (create_object_file_callback(module, file_header_data_sp, file, file_offset, file_size));
76 if (object_file_ap.get())
77 return object_file_ap.release();
78 }
79
80 // Check if this is a object container by iterating through
81 // all object container plugin instances and then trying to get
82 // an object file from the container.
83 ObjectContainerCreateInstance create_object_container_callback;
84 for (idx = 0; (create_object_container_callback = PluginManager::GetObjectContainerCreateCallbackAtIndex(idx)) != NULL; ++idx)
85 {
86 std::auto_ptr<ObjectContainer> object_container_ap(create_object_container_callback(module, file_header_data_sp, file, file_offset, file_size));
87
88 if (object_container_ap.get())
89 object_file_ap.reset (object_container_ap->GetObjectFile(file));
90
91 if (object_file_ap.get())
92 return object_file_ap.release();
93 }
94 }
95 }
96 return NULL;
97}
Jim Ingham7508e732010-08-09 23:31:02 +000098
99bool
100ObjectFile::SetModulesArchitecture (const ArchSpec &new_arch)
101{
102 return m_module->SetArchitecture (new_arch);
103}
104