1 //===-- ObjectContainerBSDArchive.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 "ObjectContainerBSDArchive.h"
11 
12 #include <ar.h>
13 
14 #include "lldb/Core/Stream.h"
15 #include "lldb/Core/ArchSpec.h"
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/PluginManager.h"
18 #include "lldb/Core/RegularExpression.h"
19 #include "lldb/Host/Mutex.h"
20 #include "lldb/Symbol/ObjectFile.h"
21 
22 using namespace lldb;
23 using namespace lldb_private;
24 
25 
26 
27 ObjectContainerBSDArchive::Object::Object() :
28     ar_name(),
29     ar_date(0),
30     ar_uid(0),
31     ar_gid(0),
32     ar_mode(0),
33     ar_size(0),
34     ar_file_offset(0),
35     ar_file_size(0)
36 {
37 }
38 
39 void
40 ObjectContainerBSDArchive::Object::Clear()
41 {
42     ar_name.Clear();
43     ar_date = 0;
44     ar_uid  = 0;
45     ar_gid  = 0;
46     ar_mode = 0;
47     ar_size = 0;
48     ar_file_offset = 0;
49     ar_file_size = 0;
50 }
51 
52 uint32_t
53 ObjectContainerBSDArchive::Object::Extract (const DataExtractor& data, uint32_t offset)
54 {
55     size_t ar_name_len = 0;
56     std::string str;
57     char *err;
58     str.assign ((const char *)data.GetData(&offset, 16),    16);
59     if (str.find("#1/") == 0)
60     {
61         // If the name is longer than 16 bytes, or contains an embedded space
62         // then it will use this format where the length of the name is
63         // here and the name characters are after this header.
64         ar_name_len = strtoul(str.c_str() + 3, &err, 10);
65     }
66     else
67     {
68         // Strip off any spaces (if the object file name contains spaces it
69         // will use the extended format above).
70         str.erase (str.find(' '));
71         ar_name.SetCString(str.c_str());
72     }
73 
74     str.assign ((const char *)data.GetData(&offset, 12),    12);
75     ar_date = strtoul(str.c_str(), &err, 10);
76 
77     str.assign ((const char *)data.GetData(&offset, 6), 6);
78     ar_uid  = strtoul(str.c_str(), &err, 10);
79 
80     str.assign ((const char *)data.GetData(&offset, 6), 6);
81     ar_gid  = strtoul(str.c_str(), &err, 10);
82 
83     str.assign ((const char *)data.GetData(&offset, 8), 8);
84     ar_mode = strtoul(str.c_str(), &err, 8);
85 
86     str.assign ((const char *)data.GetData(&offset, 10),    10);
87     ar_size = strtoul(str.c_str(), &err, 10);
88 
89     str.assign ((const char *)data.GetData(&offset, 2), 2);
90     if (str == ARFMAG)
91     {
92         if (ar_name_len > 0)
93         {
94             str.assign ((const char *)data.GetData(&offset, ar_name_len), ar_name_len);
95             ar_name.SetCString (str.c_str());
96         }
97         ar_file_offset = offset;
98         ar_file_size = ar_size - ar_name_len;
99         return offset;
100     }
101     return LLDB_INVALID_INDEX32;
102 }
103 
104 ObjectContainerBSDArchive::Archive::Archive
105 (
106     const lldb_private::ArchSpec &arch,
107     const lldb_private::TimeValue &time
108 ) :
109     m_arch (arch),
110     m_time (time),
111     m_objects()
112 {
113 }
114 
115 ObjectContainerBSDArchive::Archive::~Archive ()
116 {
117 }
118 
119 size_t
120 ObjectContainerBSDArchive::Archive::ParseObjects (DataExtractor &data)
121 {
122     std::string str;
123     uint32_t offset = 0;
124     str.assign((const char *)data.GetData(&offset, SARMAG), SARMAG);
125     if (str == ARMAG)
126     {
127         Object obj;
128         do
129         {
130             offset = obj.Extract (data, offset);
131             if (offset == LLDB_INVALID_INDEX32)
132                 break;
133             uint32_t obj_idx = m_objects.size();
134             m_objects.push_back(obj);
135             // Insert all of the C strings out of order for now...
136             m_object_name_to_index_map.Append (obj.ar_name.GetCString(), obj_idx);
137             offset += obj.ar_file_size;
138             obj.Clear();
139         } while (data.ValidOffset(offset));
140 
141         // Now sort all of the object name pointers
142         m_object_name_to_index_map.Sort ();
143     }
144     return m_objects.size();
145 }
146 
147 ObjectContainerBSDArchive::Object *
148 ObjectContainerBSDArchive::Archive::FindObject (const ConstString &object_name)
149 {
150     const UniqueCStringMap<uint32_t>::Entry *match = m_object_name_to_index_map.FindFirstValueForName (object_name.GetCString());
151     if (match)
152         return &m_objects[match->value];
153     return NULL;
154 }
155 
156 
157 ObjectContainerBSDArchive::Archive::shared_ptr
158 ObjectContainerBSDArchive::Archive::FindCachedArchive (const FileSpec &file, const ArchSpec &arch, const TimeValue &time)
159 {
160     Mutex::Locker locker(Archive::GetArchiveCacheMutex ());
161     shared_ptr archive_sp;
162     Archive::Map &archive_map = Archive::GetArchiveCache ();
163     Archive::Map::iterator pos;
164     for (pos = archive_map.find (file); pos != archive_map.end() && pos->first == file; ++pos)
165     {
166         if (pos->second->GetArchitecture() == arch &&
167             pos->second->GetModificationTime() == time)
168         {
169             archive_sp = pos->second;
170         }
171     }
172     return archive_sp;
173 }
174 
175 ObjectContainerBSDArchive::Archive::shared_ptr
176 ObjectContainerBSDArchive::Archive::ParseAndCacheArchiveForFile
177 (
178     const FileSpec &file,
179     const ArchSpec &arch,
180     const TimeValue &time,
181     DataExtractor &data
182 )
183 {
184     shared_ptr archive_sp(new Archive (arch, time));
185     if (archive_sp)
186     {
187         if (archive_sp->ParseObjects (data) > 0)
188         {
189             Mutex::Locker locker(Archive::GetArchiveCacheMutex ());
190             Archive::GetArchiveCache().insert(std::make_pair(file, archive_sp));
191         }
192         else
193         {
194             archive_sp.reset();
195         }
196     }
197     return archive_sp;
198 }
199 
200 ObjectContainerBSDArchive::Archive::Map &
201 ObjectContainerBSDArchive::Archive::GetArchiveCache ()
202 {
203     static Archive::Map g_archive_map;
204     return g_archive_map;
205 }
206 
207 Mutex &
208 ObjectContainerBSDArchive::Archive::GetArchiveCacheMutex ()
209 {
210     static Mutex g_archive_map_mutex (Mutex::eMutexTypeRecursive);
211     return g_archive_map_mutex;
212 }
213 
214 
215 void
216 ObjectContainerBSDArchive::Initialize()
217 {
218     PluginManager::RegisterPlugin (GetPluginNameStatic(),
219                                    GetPluginDescriptionStatic(),
220                                    CreateInstance);
221 }
222 
223 void
224 ObjectContainerBSDArchive::Terminate()
225 {
226     PluginManager::UnregisterPlugin (CreateInstance);
227 }
228 
229 
230 const char *
231 ObjectContainerBSDArchive::GetPluginNameStatic()
232 {
233     return "object-container.bsd-archive";
234 }
235 
236 const char *
237 ObjectContainerBSDArchive::GetPluginDescriptionStatic()
238 {
239     return "BSD Archive object container reader.";
240 }
241 
242 
243 ObjectContainer *
244 ObjectContainerBSDArchive::CreateInstance
245 (
246     Module* module,
247     DataBufferSP& data_sp,
248     const FileSpec *file,
249     addr_t offset,
250     addr_t length)
251 {
252     if (file && data_sp && ObjectContainerBSDArchive::MagicBytesMatch(data_sp))
253     {
254         Archive::shared_ptr archive_sp (Archive::FindCachedArchive (*file, module->GetArchitecture(), module->GetModificationTime()));
255 
256         if (archive_sp)
257         {
258             // We already have this archive in our cache, use it
259             std::auto_ptr<ObjectContainerBSDArchive> container_ap(new ObjectContainerBSDArchive (module, data_sp, file, offset, length));
260             if (container_ap.get())
261             {
262                 container_ap->SetArchive (archive_sp);
263                 return container_ap.release();
264             }
265         }
266 
267         // Read everything since we need that in order to index all the
268         // objects in the archive
269         data_sp = file->ReadFileContents(offset, length);
270 
271         std::auto_ptr<ObjectContainerBSDArchive> container_ap(new ObjectContainerBSDArchive (module, data_sp, file, offset, length));
272         if (container_ap->ParseHeader())
273             return container_ap.release();
274     }
275     return NULL;
276 }
277 
278 
279 
280 bool
281 ObjectContainerBSDArchive::MagicBytesMatch (DataBufferSP& dataSP)
282 {
283     DataExtractor data(dataSP, lldb::endian::InlHostByteOrder(), 4);
284     uint32_t offset = 0;
285     const char* armag = (const char* )data.PeekData (offset, sizeof(ar_hdr));
286     if (armag && ::strncmp(armag, ARMAG, SARMAG) == 0)
287     {
288         armag += offsetof(struct ar_hdr, ar_fmag) + SARMAG;
289         if (strncmp(armag, ARFMAG, 2) == 0)
290             return true;
291     }
292     return false;
293 }
294 
295 ObjectContainerBSDArchive::ObjectContainerBSDArchive
296 (
297     Module* module,
298     DataBufferSP& dataSP,
299     const lldb_private::FileSpec *file,
300     lldb::addr_t offset,
301     lldb::addr_t size
302 ) :
303     ObjectContainer (module, file, offset, size, dataSP),
304     m_archive_sp ()
305 {
306 }
307 void
308 ObjectContainerBSDArchive::SetArchive (Archive::shared_ptr &archive_sp)
309 {
310     m_archive_sp  = archive_sp;
311 }
312 
313 
314 
315 ObjectContainerBSDArchive::~ObjectContainerBSDArchive()
316 {
317 }
318 
319 bool
320 ObjectContainerBSDArchive::ParseHeader ()
321 {
322     if (m_archive_sp.get() == NULL)
323     {
324         if (m_data.GetByteSize() > 0)
325         {
326             m_archive_sp = Archive::ParseAndCacheArchiveForFile (m_file,
327                                                                  m_module->GetArchitecture(),
328                                                                  m_module->GetModificationTime(),
329                                                                  m_data);
330             // The archive might be huge, so clear "m_data" to free up the
331             // memory since it will contain the entire file (possibly more than
332             // one architecture slice). We already have an index of all objects
333             // in the file, so we will be ready to serve up those objects.
334             m_data.Clear();
335         }
336     }
337     return m_archive_sp.get() != NULL;
338 }
339 
340 void
341 ObjectContainerBSDArchive::Dump (Stream *s) const
342 {
343     s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
344     s->Indent();
345     const size_t num_archs = GetNumArchitectures();
346     const size_t num_objects = GetNumObjects();
347     s->Printf("ObjectContainerBSDArchive, num_archs = %u, num_objects = %u", num_archs, num_objects);
348     uint32_t i;
349     ArchSpec arch;
350     s->IndentMore();
351     for (i=0; i<num_archs; i++)
352     {
353         s->Indent();
354         GetArchitectureAtIndex(i, arch);
355         s->Printf("arch[%u] = %s\n", arch.GetArchitectureName());
356     }
357     for (i=0; i<num_objects; i++)
358     {
359         s->Indent();
360         s->Printf("object[%u] = %s\n", GetObjectNameAtIndex (i));
361     }
362     s->IndentLess();
363     s->EOL();
364 }
365 
366 ObjectFile *
367 ObjectContainerBSDArchive::GetObjectFile (const FileSpec *file)
368 {
369     if (m_module->GetObjectName() && m_archive_sp)
370     {
371         Object *object = m_archive_sp->FindObject (m_module->GetObjectName());
372         if (object)
373             return ObjectFile::FindPlugin (m_module, file, m_offset + object->ar_file_offset, object->ar_file_size);
374     }
375     return NULL;
376 }
377 
378 
379 //------------------------------------------------------------------
380 // PluginInterface protocol
381 //------------------------------------------------------------------
382 const char *
383 ObjectContainerBSDArchive::GetPluginName()
384 {
385     return "object-container.bsd-archive";
386 }
387 
388 const char *
389 ObjectContainerBSDArchive::GetShortPluginName()
390 {
391     return GetPluginNameStatic();
392 }
393 
394 uint32_t
395 ObjectContainerBSDArchive::GetPluginVersion()
396 {
397     return 1;
398 }
399 
400