There seems to be a bug in DwarfUnit which one can easily step on when building boost examples with full LTO.
It causes template function declaration inside a class to have invalid DW_AT_decl_file attribute and GNU
objdump and addr2line tools to report multiple failures while processing such executables.
Assume we have following C++ structure:
struct S { int foo; template< class T > T squared(T value) { return value * value; } };
Even though this structure contains two elements, debug information for it will contain only one,
because squared may not be used anywhere in the program and so may not even be compiled.
!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, ....) !18 = distinct !DICompositeType(tag: DW_TAG_class_type, name: "S", ...., elements !19, ....) !19 = !{!20} !20 = !DIDerivedType(tag: DW_TAG_member, name: "foo", ....)
If some CU in a program is using S::squared two records will be added to debug information: definition and declaration
The latter will reference struct S via scope field:
!37 = distinct !DISubprogram(name: "squared<int>", unit !2, declaration !38, .....) !38 = distinct !DISubprogram(name: "squared<int>", scope !18, ....)
The struct S could be used in multiple CUs, some of them may use squared and some may not. When full LTO is used
IRMover will combine debug information from all CUs merging the types and DIE corresponding to declaration of squared
my be processed by DwarfUnit instance corresponding to CU#1 and DIE corresponding to its scope (struct S) may be owned
by CU#2. In such case if we add source line attributes for declaration on behalf of CU#1 we'll likely break the debug info,
because file ids in CU#1 and CU#2 don't match.
This patch fixes the issue by adding debug info for declaration on behalf of owning CU.