ObjectFile.cpp revision 24943d2ee8bfaa7cf5893e4709143924157a5c1e
1//===-- 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                    {
54                        archive_file.SetFile (path.c_str());
55                        file_size = archive_file.GetByteSize();
56                        if (file_size > 0)
57                            module->SetFileSpecAndObjectName (archive_file, ConstString(object.c_str()));
58                    }
59                }
60            }
61
62            DataBufferSP file_header_data_sp(file->ReadFileContents(file_offset, 512));
63            uint32_t idx;
64
65            // Check if this is a normal object file by iterating through
66            // all object file plugin instances.
67            ObjectFileCreateInstance create_object_file_callback;
68            for (idx = 0; (create_object_file_callback = PluginManager::GetObjectFileCreateCallbackAtIndex(idx)) != NULL; ++idx)
69            {
70                object_file_ap.reset (create_object_file_callback(module, file_header_data_sp, file, file_offset, file_size));
71                if (object_file_ap.get())
72                    return object_file_ap.release();
73            }
74
75            // Check if this is a object container by iterating through
76            // all object container plugin instances and then trying to get
77            // an object file from the container.
78            ObjectContainerCreateInstance create_object_container_callback;
79            for (idx = 0; (create_object_container_callback = PluginManager::GetObjectContainerCreateCallbackAtIndex(idx)) != NULL; ++idx)
80            {
81                std::auto_ptr<ObjectContainer> object_container_ap(create_object_container_callback(module, file_header_data_sp, file, file_offset, file_size));
82
83                if (object_container_ap.get())
84                    object_file_ap.reset (object_container_ap->GetObjectFile(file));
85
86                if (object_file_ap.get())
87                    return object_file_ap.release();
88            }
89        }
90    }
91    return NULL;
92}
93