]>
git.saurik.com Git - apple/ld64.git/blob - src/ld/Snapshot.h
5 // Created by Josh Behnke on 8/25/11.
6 // Copyright (c) 2011 Apple Inc. All rights reserved.
9 #ifndef ld64_Snapshot_h
10 #define ld64_Snapshot_h
19 class SnapshotLogItem
;
24 static Snapshot
*globalSnapshot
;
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
32 Snapshot(const Options
* opts
);
35 // Control the data captured in the snapshot
36 void setSnapshotMode(SnapshotMode mode
);
38 // Use the basename of path to construct the snapshot name.
39 // Must be called prior to createSnapshot().
40 void setOutputPath(const char *path
);
42 // Set the directory in which the snapshot will be created.
43 // Must be called prior to createSnapshot().
44 void setSnapshotPath(const char *path
);
46 // Stores the linker command line in the snapshot
47 void recordRawArgs(int argc
, const char *argv
[]);
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);
57 // record the -arch string
58 void recordArch(const char *arch
);
60 // Stores an object file in the snapshot, using a unique name in an "objects" subdir.
61 void recordObjectFile(const char *path
);
63 // Records symbol names used in dylibs. Does not store anything in the snapshot.
64 void recordDylibSymbol(ld::dylib::File
* dylibFile
, const char *name
);
66 // Stores an archive (.a) file in the snapshot.
67 void recordArchive(const char *archiveFile
);
69 // Copies the framework binary into the snapshot frameworks directory.
70 void recordSubUmbrella(const char *frameworkPath
);
72 // Copies the library binary into the snapshot dylibs directory.
73 void recordSubLibrary(const char *dylibPath
);
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
, ...);
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();
85 // Returns the snapshot root directory.
86 const char *rootDir() { return fRootDir
; }
90 friend class SnapshotArchiveFileLog
;
92 typedef std::vector
<void(^)(void)> SnapshotLog
;
95 bool operator() (const char *a
, const char *b
) const { return ::strcmp(a
, b
) < 0; }
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
;
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);
108 void setSnapshotName();
111 const char * subdir(const char *subdir
);
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
);
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
);
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
);
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; }
132 // returns true if the snapshot has not been created (by createSnapshot()) yet
133 bool isLazy() { return fRootDir
== NULL
; }
135 void addFrameworkArg(const char *framework
);
136 void addDylibArg(const char *dylib
);
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
;
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
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
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