14
14
#include " llvm/CodeGen/AccelTable.h"
15
15
#include " llvm/CodeGen/NonRelocatableStringpool.h"
16
16
#include " llvm/DWARFLinker/DWARFLinkerCompileUnit.h"
17
+ #include " llvm/DebugInfo/DWARF/DWARFContext.h"
17
18
#include " llvm/DebugInfo/DWARF/DWARFDebugLine.h"
18
19
#include " llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
19
20
#include " llvm/DebugInfo/DWARF/DWARFDie.h"
20
21
#include " llvm/DebugInfo/DWARF/DWARFExpression.h"
21
22
#include < map>
22
23
23
24
namespace llvm {
24
- class DWARFContext ;
25
25
class DWARFExpression ;
26
26
class DWARFUnit ;
27
27
class DataExtractor ;
@@ -30,13 +30,6 @@ template <typename T> class SmallVectorImpl;
30
30
31
31
enum class DwarfLinkerClient { Dsymutil, LLD, General };
32
32
33
- // / The kind of accelerator tables we should emit.
34
- enum class DwarfLinkerAccelTableKind : uint8_t {
35
- Apple, // /< .apple_names, .apple_namespaces, .apple_types, .apple_objc.
36
- Pub, // /< .debug_pubnames, .debug_pubtypes
37
- DebugNames // /< .debug_names.
38
- };
39
-
40
33
// / AddressesMap represents information about valid addresses used
41
34
// / by debug information. Valid addresses are those which points to
42
35
// / live code sections. i.e. relocations for these addresses point
@@ -221,39 +214,48 @@ class DwarfEmitter {
221
214
222
215
// / Returns size of generated .debug_loclists section.
223
216
virtual uint64_t getLocListsSectionSize () const = 0;
217
+
218
+ // / Dump the file to the disk.
219
+ virtual void finish () = 0;
220
+
221
+ // / Emit the swift_ast section stored in \p Buffer.
222
+ virtual void emitSwiftAST (StringRef Buffer) = 0;
223
+
224
+ // / Emit the swift reflection section stored in \p Buffer.
225
+ virtual void emitSwiftReflectionSection (
226
+ llvm::binaryformat::Swift5ReflectionSectionKind ReflSectionKind,
227
+ StringRef Buffer, uint32_t Alignment, uint32_t Size) = 0;
228
+
229
+ // / Returns underlying AsmPrinter.
230
+ virtual AsmPrinter &getAsmPrinter () const = 0;
224
231
};
225
232
233
+ class DwarfStreamer ;
226
234
using UnitListTy = std::vector<std::unique_ptr<CompileUnit>>;
227
235
228
236
// / This class represents DWARF information for source file
229
237
// / and its address map.
230
238
class DWARFFile {
231
239
public:
232
- DWARFFile (StringRef Name, DWARFContext *Dwarf, AddressesMap *Addresses,
240
+ DWARFFile (StringRef Name, std::unique_ptr<DWARFContext> Dwarf,
241
+ std::unique_ptr<AddressesMap> Addresses,
233
242
const std::vector<std::string> &Warnings)
234
- : FileName(Name), Dwarf(Dwarf), Addresses(Addresses), Warnings(Warnings) {
235
- }
243
+ : FileName(Name), Dwarf(std::move( Dwarf)),
244
+ Addresses (std::move(Addresses)), Warnings(Warnings) { }
236
245
237
246
// / The object file name.
238
247
StringRef FileName;
239
248
240
249
// / The source DWARF information.
241
- DWARFContext * Dwarf = nullptr ;
250
+ std::unique_ptr< DWARFContext> Dwarf;
242
251
243
252
// / Helpful address information(list of valid address ranges, relocations).
244
- AddressesMap * Addresses = nullptr ;
253
+ std::unique_ptr< AddressesMap> Addresses;
245
254
246
255
// / Warnings for this object file.
247
256
const std::vector<std::string> &Warnings;
248
257
};
249
258
250
- typedef std::function<void (const Twine &Warning, StringRef Context,
251
- const DWARFDie *DIE)>
252
- messageHandler;
253
- typedef std::function<void (const DWARFFile &File)> inputVerificationHandler;
254
- typedef std::function<ErrorOr<DWARFFile &>(StringRef ContainerName,
255
- StringRef Path)>
256
- objFileLoader;
257
259
typedef std::map<std::string, std::string> swiftInterfacesMap;
258
260
typedef std::map<std::string, std::string> objectPrefixMap;
259
261
@@ -275,9 +277,43 @@ typedef function_ref<void(const DWARFUnit &Unit)> CompileUnitHandler;
275
277
// / processing a object file.
276
278
class DWARFLinker {
277
279
public:
278
- DWARFLinker (DwarfEmitter *Emitter,
279
- DwarfLinkerClient ClientID = DwarfLinkerClient::General)
280
- : TheDwarfEmitter(Emitter), DwarfLinkerClientID(ClientID) {}
280
+ typedef std::function<void (const Twine &Warning, StringRef Context,
281
+ const DWARFDie *DIE)>
282
+ messageHandler;
283
+ DWARFLinker (messageHandler ErrorHandler, messageHandler WarningHandler,
284
+ std::function<StringRef(StringRef)> StringsTranslator)
285
+ : DwarfLinkerClientID(DwarfLinkerClient::Dsymutil),
286
+ StringsTranslator (StringsTranslator), ErrorHandler(ErrorHandler),
287
+ WarningHandler(WarningHandler) {}
288
+
289
+ static std::unique_ptr<DWARFLinker> createLinker (
290
+ messageHandler ErrorHandler, messageHandler WarningHandler,
291
+ std::function<StringRef(StringRef)> StringsTranslator = nullptr) {
292
+ return std::make_unique<DWARFLinker>(ErrorHandler, WarningHandler,
293
+ StringsTranslator);
294
+ }
295
+
296
+ // / Type of output file.
297
+ enum class OutputFileType {
298
+ Object,
299
+ Assembly,
300
+ };
301
+
302
+ // / The kind of accelerator tables we should emit.
303
+ enum class AccelTableKind : uint8_t {
304
+ Apple, // /< .apple_names, .apple_namespaces, .apple_types, .apple_objc.
305
+ Pub, // /< .debug_pubnames, .debug_pubtypes
306
+ DebugNames // /< .debug_names.
307
+ };
308
+ typedef std::function<void (const DWARFFile &File)> inputVerificationHandler;
309
+ typedef std::function<ErrorOr<DWARFFile &>(StringRef ContainerName,
310
+ StringRef Path)>
311
+ objFileLoader;
312
+
313
+ Error createEmitter (const Triple &TheTriple, OutputFileType FileType,
314
+ raw_pwrite_stream &OutFile);
315
+
316
+ DwarfEmitter *getEmitter ();
281
317
282
318
// / Add object file to be linked. Pre-load compile unit die. Call
283
319
// / \p OnCUDieLoaded for each compile unit die. If specified \p File
@@ -289,8 +325,7 @@ class DWARFLinker {
289
325
DWARFFile &File, objFileLoader Loader = nullptr ,
290
326
CompileUnitHandler OnCUDieLoaded = [](const DWARFUnit &) {});
291
327
292
- // / Link debug info for added objFiles. Object
293
- // / files are linked all together.
328
+ // / Link debug info for added objFiles. Object files are linked all together.
294
329
Error link ();
295
330
296
331
// / A number of methods setting various linking options:
@@ -304,14 +339,15 @@ class DWARFLinker {
304
339
// / Verify the input DWARF.
305
340
void setVerifyInputDWARF (bool Verify) { Options.VerifyInputDWARF = Verify; }
306
341
307
- // / Do not emit linked dwarf info.
308
- void setNoOutput (bool NoOut) { Options.NoOutput = NoOut; }
309
-
310
342
// / Do not unique types according to ODR.
311
343
void setNoODR (bool NoODR) { Options.NoODR = NoODR; }
312
344
313
- // / update existing DWARF info(for the linked binary).
314
- void setUpdate (bool Update) { Options.Update = Update; }
345
+ // / Update index tables only(do not modify rest of DWARF).
346
+ void setUpdateIndexTablesOnly (bool Update) { Options.Update = Update; }
347
+
348
+ // / Allow generating valid, but non-deterministic output.
349
+ void setAllowNonDeterministicOutput (bool ) { /* Nothing to do. */
350
+ }
315
351
316
352
// / Set whether to keep the enclosing function for a static variable.
317
353
void setKeepFunctionForStatic (bool KeepFunctionForStatic) {
@@ -322,7 +358,7 @@ class DWARFLinker {
322
358
void setNumThreads (unsigned NumThreads) { Options.Threads = NumThreads; }
323
359
324
360
// / Add kind of accelerator tables to be generated.
325
- void addAccelTableKind (DwarfLinkerAccelTableKind Kind) {
361
+ void addAccelTableKind (AccelTableKind Kind) {
326
362
assert (std::find (Options.AccelTables .begin (), Options.AccelTables .end (),
327
363
Kind) == Options.AccelTables .end ());
328
364
Options.AccelTables .emplace_back (Kind);
@@ -331,27 +367,11 @@ class DWARFLinker {
331
367
// / Set prepend path for clang modules.
332
368
void setPrependPath (const std::string &Ppath) { Options.PrependPath = Ppath; }
333
369
334
- // / Set translator which would be used for strings.
335
- void
336
- setStringsTranslator (std::function<StringRef(StringRef)> StringsTranslator) {
337
- this ->StringsTranslator = StringsTranslator;
338
- }
339
-
340
370
// / Set estimated objects files amount, for preliminary data allocation.
341
371
void setEstimatedObjfilesAmount (unsigned ObjFilesNum) {
342
372
ObjectContexts.reserve (ObjFilesNum);
343
373
}
344
374
345
- // / Set warning handler which would be used to report warnings.
346
- void setWarningHandler (messageHandler Handler) {
347
- Options.WarningHandler = Handler;
348
- }
349
-
350
- // / Set error handler which would be used to report errors.
351
- void setErrorHandler (messageHandler Handler) {
352
- Options.ErrorHandler = Handler;
353
- }
354
-
355
375
// / Set verification handler which would be used to report verification
356
376
// / errors.
357
377
void setInputVerificationHandler (inputVerificationHandler Handler) {
@@ -370,7 +390,7 @@ class DWARFLinker {
370
390
371
391
// / Set target DWARF version.
372
392
Error setTargetDWARFVersion (uint16_t TargetDWARFVersion) {
373
- if (TargetDWARFVersion < 1 || TargetDWARFVersion > 5 )
393
+ if (( TargetDWARFVersion < 1 ) || ( TargetDWARFVersion > 5 ) )
374
394
return createStringError (std::errc::invalid_argument,
375
395
" unsupported DWARF version: %d" ,
376
396
TargetDWARFVersion);
@@ -444,14 +464,14 @@ class DWARFLinker {
444
464
445
465
void reportWarning (const Twine &Warning, const DWARFFile &File,
446
466
const DWARFDie *DIE = nullptr ) const {
447
- if (Options. WarningHandler != nullptr )
448
- Options. WarningHandler (Warning, File.FileName , DIE);
467
+ if (WarningHandler != nullptr )
468
+ WarningHandler (Warning, File.FileName , DIE);
449
469
}
450
470
451
471
void reportError (const Twine &Warning, const DWARFFile &File,
452
472
const DWARFDie *DIE = nullptr ) const {
453
- if (Options. ErrorHandler != nullptr )
454
- Options. ErrorHandler (Warning, File.FileName , DIE);
473
+ if (ErrorHandler != nullptr )
474
+ ErrorHandler (Warning, File.FileName , DIE);
455
475
}
456
476
457
477
// / Emit warnings as Dwarf compile units to leave a trail after linking.
@@ -799,7 +819,7 @@ class DWARFLinker {
799
819
BumpPtrAllocator DIEAlloc;
800
820
// / @}
801
821
802
- DwarfEmitter * TheDwarfEmitter;
822
+ std::unique_ptr<DwarfStreamer> TheDwarfEmitter;
803
823
std::vector<LinkContext> ObjectContexts;
804
824
805
825
// / The CIEs that have been emitted in the output section. The actual CIE
@@ -828,6 +848,12 @@ class DWARFLinker {
828
848
// / A unique ID that identifies each compile unit.
829
849
unsigned UniqueUnitID = 0 ;
830
850
851
+ // error handler
852
+ messageHandler ErrorHandler = nullptr ;
853
+
854
+ // warning handler
855
+ messageHandler WarningHandler = nullptr ;
856
+
831
857
// / linking options
832
858
struct DWARFLinkerOptions {
833
859
// / DWARF version for the output.
@@ -842,9 +868,6 @@ class DWARFLinker {
842
868
// / Verify the input DWARF.
843
869
bool VerifyInputDWARF = false ;
844
870
845
- // / Skip emitting output
846
- bool NoOutput = false ;
847
-
848
871
// / Do not unique types according to ODR
849
872
bool NoODR = false ;
850
873
@@ -859,17 +882,11 @@ class DWARFLinker {
859
882
unsigned Threads = 1 ;
860
883
861
884
// / The accelerator table kinds
862
- SmallVector<DwarfLinkerAccelTableKind , 1 > AccelTables;
885
+ SmallVector<AccelTableKind , 1 > AccelTables;
863
886
864
887
// / Prepend path for the clang modules.
865
888
std::string PrependPath;
866
889
867
- // warning handler
868
- messageHandler WarningHandler = nullptr ;
869
-
870
- // error handler
871
- messageHandler ErrorHandler = nullptr ;
872
-
873
890
// input verification handler
874
891
inputVerificationHandler InputVerificationHandler = nullptr ;
875
892
0 commit comments