]> git.saurik.com Git - apple/ld64.git/blob - src/ld/Snapshot.h
ld64-409.12.tar.gz
[apple/ld64.git] / src / ld / Snapshot.h
1 //
2 // Snapshot.h
3 // ld64
4 //
5 // Created by Josh Behnke on 8/25/11.
6 // Copyright (c) 2011 Apple Inc. All rights reserved.
7 //
8
9 #ifndef ld64_Snapshot_h
10 #define ld64_Snapshot_h
11 #include <stdint.h>
12 #include <string.h>
13 #include <map>
14 #include <vector>
15
16 #include "ld.hpp"
17
18 class Options;
19 class SnapshotLogItem;
20
21 class Snapshot {
22
23 public:
24 static Snapshot *globalSnapshot;
25
26 typedef enum {
27 SNAPSHOT_DISABLED, // nothing is recorded
28 SNAPSHOT_DEBUG, // records: .o, .dylib, .framework, .a, and other data files
29 SNAPSHOT_KEXT, // records: .o, .a, and other data files
30 } SnapshotMode;
31
32 Snapshot(const Options * opts);
33 ~Snapshot();
34
35 // Control the data captured in the snapshot
36 void setSnapshotMode(SnapshotMode mode);
37
38 // Use the basename of path to construct the snapshot name.
39 // Must be called prior to createSnapshot().
40 void setOutputPath(const char *path);
41
42 // Set the directory in which the snapshot will be created.
43 // Must be called prior to createSnapshot().
44 void setSnapshotPath(const char *path);
45
46 // Stores the linker command line in the snapshot
47 void recordRawArgs(int argc, const char *argv[]);
48
49 // Adds one or more args to the snapshot link command.
50 // argIndex is the index in the original raw args vector to start adding args
51 // argCount is the count of args to copy from the raw args vector
52 // fileArg is the index relative to argIndex of a file arg. The file is copied into the
53 // snapshot and the path is fixed up in the snapshot link command. (skipped if fileArg==-1)
54 // recordRawArgs() must be called prior to the first call to addSnapshotLinkArg()
55 void addSnapshotLinkArg(int argIndex, int argCount=1, int fileArg=-1);
56
57 // record the -arch string
58 void recordArch(const char *arch);
59
60 // Stores an object file in the snapshot, using a unique name in an "objects" subdir.
61 void recordObjectFile(const char *path);
62
63 // Records symbol names used in dylibs. Does not store anything in the snapshot.
64 void recordDylibSymbol(ld::dylib::File* dylibFile, const char *name);
65
66 // Stores an archive (.a) file in the snapshot.
67 void recordArchive(const char *archiveFile);
68
69 // Copies the framework binary into the snapshot frameworks directory.
70 void recordSubUmbrella(const char *frameworkPath);
71
72 // Copies the library binary into the snapshot dylibs directory.
73 void recordSubLibrary(const char *dylibPath);
74
75 // Records arbitrary text messages into a log file in the snapshot.
76 // Used by the assertion failure machienery.
77 void recordAssertionMessage(const char *fmt, ...);
78
79 // Create the snapshot.
80 // Until this is called the snapshot operates lazily, storing minimal data in memory.
81 // When this is called the snapshot is created and any previously recorded data is
82 // immediately copied. Any subsequent additions to the snapshot are copied immediately.
83 void createSnapshot();
84
85 // Returns the snapshot root directory.
86 const char *rootDir() { return fRootDir; }
87
88 private:
89
90 friend class SnapshotArchiveFileLog;
91
92 typedef std::vector<void(^)(void)> SnapshotLog;
93
94 struct strcompclass {
95 bool operator() (const char *a, const char *b) const { return ::strcmp(a, b) < 0; }
96 };
97 typedef std::vector<const char *> StringVector;
98 typedef std::map<const char *, int, strcompclass > DylibMap;
99 typedef std::map<const char *, const char *, strcompclass> PathMap;
100 typedef std::vector<unsigned> IntVector;
101
102 // Write the current contents of the args vector to a file in the snapshot.
103 // If filename is NULL then "link_command" is used.
104 // This is used to write both the original and the "cooked" versions of the link command
105 void writeCommandLine(bool rawArgs=false);
106
107 //
108 void setSnapshotName();
109
110 //
111 const char * subdir(const char *subdir);
112
113 // Construct a path in the snapshot.
114 // buf is a sring buffer in which the path is constructed
115 // subdir is an optional subdirectory, and file is a file name
116 // Constructs the path <snapshot_root>/<subdir>/<file> in buf
117 void buildPath(char *buf, const char *subdir, const char *file);
118
119 // Similar to buildPath(), except this ensures the returned path
120 // does not reference an existing file in the snapshot.
121 // Performs uniquing by appending a count suffex to the path (ie .../file-XX)
122 void buildUniquePath(char *buf, const char *subdir, const char *file);
123
124 // Copies an arbitrary file to the snapshot. Subdir specifies an optional subdirectory name.
125 // Uses buildUniquePath to construct a unique path. If the result path is needed by the caller
126 // then a path buffer can be supplied in buf. Otherwise an internal buffer is used.
127 void copyFileToSnapshot(const char *sourcePath, const char *subdir, char *buf=NULL);
128
129 // Convert a full path to snapshot relative by constructing an interior pointer at the right offset.
130 const char *snapshotRelativePath(const char *path) { return path+strlen(fRootDir)+1; }
131
132 // returns true if the snapshot has not been created (by createSnapshot()) yet
133 bool isLazy() { return fRootDir == NULL; }
134
135 void addFrameworkArg(const char *framework);
136 void addDylibArg(const char *dylib);
137
138 const Options * fOptions;
139 SnapshotLog fLog; // log of events that recorded data in a snapshot prior to createSnapshot()
140 bool fRecordArgs; // record command line
141 bool fRecordObjects; // record .o files
142 bool fRecordDylibSymbols; // record referenced dylib/framework symbols
143 bool fRecordArchiveFiles; // record .a files
144 bool fRecordUmbrellaFiles; // record re-exported sub frameworks/dylibs
145 bool fRecordDataFiles; // record other data files
146 bool fFrameworkArgAdded;
147 bool fRecordKext;
148
149 const char *fSnapshotLocation; // parent directory of frootDir
150 const char *fSnapshotName; // a string to use in constructing the snapshot name
151 const char *fOutputPath; // -o path
152 char *fRootDir; // root directory of the snapshot
153 const char *fArchString;
154 int fFilelistFile; // file descriptor to the open text file used for the -filelist
155
156 StringVector fRawArgs; // stores the raw command line args
157 StringVector fArgs; // stores the "cooked" command line args
158 IntVector fArgIndicies; // where args start in fArgs
159 PathMap fPathMap; // mapping of original paths->snapshot paths for copied files
160
161 DylibMap fDylibSymbols; // map of dylib names to string vector containing referenced symbol names
162 StringVector *fCopiedArchives; // vector of .a files that have been copied to the snapshot
163 };
164
165 #endif