1 : // Copyright 2014 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // Declares a class for writing an archive of COFF object files to a .lib
16 : // file. See ar_reader.h for details of MSVS version of the file format.
17 :
18 : #ifndef SYZYGY_AR_AR_WRITER_H_
19 : #define SYZYGY_AR_AR_WRITER_H_
20 :
21 : #include <set>
22 :
23 : #include "base/files/file_path.h"
24 : #include "base/memory/scoped_vector.h"
25 : #include "syzygy/ar/ar_common.h"
26 :
27 : namespace ar {
28 :
29 : // Class for writing an archive of COFF object files. This mimics the behaviour
30 : // of lib.exe in that duplicate symbol definitions are ignored but allowed, with
31 : // the first definition being the one that is exported to the symbol table.
32 : class ArWriter {
33 : public:
34 : typedef std::pair<ParsedArFileHeader, const DataBuffer*> File;
35 : typedef std::vector<File> FileVector;
36 :
37 : ArWriter();
38 :
39 : // @returns the current list of files that will be added to the archive.
40 E : const FileVector& files() const { return files_; }
41 :
42 : // @returns the current set of exported symbols.
43 E : const SymbolIndexMap& symbols() const { return symbols_; }
44 :
45 : // Schedules the given object file to be added to the archive.
46 : // @param filename The filename that will be associated with the content.
47 : // @param timestamp The timestamp to be associated with the file.
48 : // @param mode The mode to be associated with the file. In the same format
49 : // as ST_MODE from _wstat.
50 : // @param contents The contents of the file. The lifetime of this object
51 : // must exceed the lifetime of the writer.
52 : // @param path The file to be added; the filename as specified in @p path
53 : // will be used, and the contents read from disk. Uses the timestamp and
54 : // mode of the file on disk.
55 : // @returns true on success, false otherwise.
56 : bool AddFile(const base::StringPiece& filename,
57 : const base::Time& timestamp,
58 : uint32 mode,
59 : const DataBuffer* contents);
60 : bool AddFile(const base::FilePath& path);
61 :
62 : // Writes the current set of files to an archive at the specified @p path.
63 : // @param path The path of the archive file to be written.
64 : // @returns true on success, false otherwise.
65 : bool Write(const base::FilePath& path);
66 :
67 : protected:
68 : typedef std::set<std::pair<std::string, size_t>> FileIndexMap;
69 : typedef ScopedVector<DataBuffer> ScopedDataBuffers;
70 :
71 : // Contains a collection of object files and the names with which they
72 : // will be committed to the archive.
73 : FileVector files_;
74 :
75 : // A multimap of filenames to their indices in |files_|.
76 : FileIndexMap file_index_map_;
77 :
78 : // Any files whose contents have been read by the writer are stored here.
79 : ScopedDataBuffers buffers_;
80 :
81 : // The collection of symbols exported from the various object files.
82 : SymbolIndexMap symbols_;
83 :
84 : // The collection of weak symbols currently exported from the various object
85 : // files. These have to be tracked separately as they can be overridden by
86 : // later object files.
87 : SymbolIndexMap weak_symbols_;
88 :
89 : private:
90 : DISALLOW_COPY_AND_ASSIGN(ArWriter);
91 : };
92 :
93 : } // namespace ar
94 :
95 : #endif // SYZYGY_AR_AR_WRITER_H_
|