1*1cf9926bSpatrick--warn-backrefs 2*1cf9926bSpatrick=============== 3*1cf9926bSpatrick 4*1cf9926bSpatrick``--warn-backrefs`` gives a warning when an undefined symbol reference is 5*1cf9926bSpatrickresolved by a definition in an archive to the left of it on the command line. 6*1cf9926bSpatrick 7*1cf9926bSpatrickA linker such as GNU ld makes a single pass over the input files from left to 8*1cf9926bSpatrickright maintaining the set of undefined symbol references from the files loaded 9*1cf9926bSpatrickso far. When encountering an archive or an object file surrounded by 10*1cf9926bSpatrick``--start-lib`` and ``--end-lib`` that archive will be searched for resolving 11*1cf9926bSpatricksymbol definitions; this may result in input files being loaded, updating the 12*1cf9926bSpatrickset of undefined symbol references. When all resolving definitions have been 13*1cf9926bSpatrickloaded from the archive, the linker moves on the next file and will not return 14*1cf9926bSpatrickto it. This means that if an input file to the right of a archive cannot have 15*1cf9926bSpatrickan undefined symbol resolved by a archive to the left of it. For example: 16*1cf9926bSpatrick 17*1cf9926bSpatrick ld def.a ref.o 18*1cf9926bSpatrick 19*1cf9926bSpatrickwill result in an ``undefined reference`` error. If there are no cyclic 20*1cf9926bSpatrickreferences, the archives can be ordered in such a way that there are no 21*1cf9926bSpatrickbackward references. If there are cyclic references then the ``--start-group`` 22*1cf9926bSpatrickand ``--end-group`` options can be used, or the same archive can be placed on 23*1cf9926bSpatrickthe command line twice. 24*1cf9926bSpatrick 25*1cf9926bSpatrickLLD remembers the symbol table of archives that it has previously seen, so if 26*1cf9926bSpatrickthere is a reference from an input file to the right of an archive, LLD will 27*1cf9926bSpatrickstill search that archive for resolving any undefined references. This means 28*1cf9926bSpatrickthat an archive only needs to be included once on the command line and the 29*1cf9926bSpatrick``--start-group`` and ``--end-group`` options are redundant. 30*1cf9926bSpatrick 31*1cf9926bSpatrickA consequence of the differing archive searching semantics is that the same 32*1cf9926bSpatricklinker command line can result in different outcomes. A link may succeed with 33*1cf9926bSpatrickLLD that will fail with GNU ld, or even worse both links succeed but they have 34*1cf9926bSpatrickselected different objects from different archives that both define the same 35*1cf9926bSpatricksymbols. 36*1cf9926bSpatrick 37*1cf9926bSpatrickThe ``warn-backrefs`` option provides information that helps identify cases 38*1cf9926bSpatrickwhere LLD and GNU ld archive selection may differ. 39*1cf9926bSpatrick 40*1cf9926bSpatrick | % ld.lld --warn-backrefs ... -lB -lA 41*1cf9926bSpatrick | ld.lld: warning: backward reference detected: system in A.a(a.o) refers to B.a(b.o) 42*1cf9926bSpatrick 43*1cf9926bSpatrick | % ld.lld --warn-backrefs ... --start-lib B/b.o --end-lib --start-lib A/a.o --end-lib 44*1cf9926bSpatrick | ld.lld: warning: backward reference detected: system in A/a.o refers to B/b.o 45*1cf9926bSpatrick 46*1cf9926bSpatrick # To suppress the warning, you can specify --warn-backrefs-exclude=<glob> to match B/b.o or B.a(b.o) 47*1cf9926bSpatrick 48*1cf9926bSpatrickThe ``--warn-backrefs`` option can also provide a check to enforce a 49*1cf9926bSpatricktopological order of archives, which can be useful to detect layering 50*1cf9926bSpatrickviolations (albeit unable to catch all cases). There are two cases where GNU ld 51*1cf9926bSpatrickwill result in an ``undefined reference`` error: 52*1cf9926bSpatrick 53*1cf9926bSpatrick* If adding the dependency does not form a cycle: conceptually ``A`` is higher 54*1cf9926bSpatrick level library while ``B`` is at a lower level. When you are developing an 55*1cf9926bSpatrick application ``P`` which depends on ``A``, but does not directly depend on 56*1cf9926bSpatrick ``B``, your link may fail surprisingly with ``undefined symbol: 57*1cf9926bSpatrick symbol_defined_in_B`` if the used/linked part of ``A`` happens to need some 58*1cf9926bSpatrick components of ``B``. It is inappropriate for ``P`` to add a dependency on 59*1cf9926bSpatrick ``B`` since ``P`` does not use ``B`` directly. 60*1cf9926bSpatrick* If adding the dependency forms a cycle, e.g. ``B->C->A ~> B``. ``A`` 61*1cf9926bSpatrick is supposed to be at the lowest level while ``B`` is supposed to be at the 62*1cf9926bSpatrick highest level. When you are developing ``C_test`` testing ``C``, your link may 63*1cf9926bSpatrick fail surprisingly with ``undefined symbol`` if there is somehow a dependency on 64*1cf9926bSpatrick some components of ``B``. You could fix the issue by adding the missing 65*1cf9926bSpatrick dependency (``B``), however, then every test (``A_test``, ``B_test``, 66*1cf9926bSpatrick ``C_test``) will link against every library. This breaks the motivation 67*1cf9926bSpatrick of splitting ``B``, ``C`` and ``A`` into separate libraries and makes binaries 68*1cf9926bSpatrick unnecessarily large. Moreover, the layering violation makes lower-level 69*1cf9926bSpatrick libraries (e.g. ``A``) vulnerable to changes to higher-level libraries (e.g. 70*1cf9926bSpatrick ``B``, ``C``). 71*1cf9926bSpatrick 72*1cf9926bSpatrickResolution: 73*1cf9926bSpatrick 74*1cf9926bSpatrick* Add a dependency from ``A`` to ``B``. 75*1cf9926bSpatrick* The reference may be unintended and can be removed. 76*1cf9926bSpatrick* The dependency may be intentionally omitted because there are multiple 77*1cf9926bSpatrick libraries like ``B``. Consider linking ``B`` with object semantics by 78*1cf9926bSpatrick surrounding it with ``--whole-archive`` and ``--no-whole-archive``. 79*1cf9926bSpatrick* In the case of circular dependency, sometimes merging the libraries are the best. 80*1cf9926bSpatrick 81*1cf9926bSpatrickThere are two cases like a library sandwich where GNU ld will select a 82*1cf9926bSpatrickdifferent object. 83*1cf9926bSpatrick 84*1cf9926bSpatrick* ``A.a B A2.so``: ``A.a`` may be used as an interceptor (e.g. it provides some 85*1cf9926bSpatrick optimized libc functions and ``A2`` is libc). ``B`` does not need to know 86*1cf9926bSpatrick about ``A.a``, and ``A.a`` may be pulled into the link by other part of the 87*1cf9926bSpatrick program. For linker portability, consider ``--whole-archive`` and 88*1cf9926bSpatrick ``--no-whole-archive``. 89*1cf9926bSpatrick 90*1cf9926bSpatrick* ``A.a B A2.a``: similar to the above case but ``--warn-backrefs`` does not 91*1cf9926bSpatrick flag the problem, because ``A2.a`` may be a replicate of ``A.a``, which is 92*1cf9926bSpatrick redundant but benign. In some cases ``A.a`` and ``B`` should be surrounded by 93*1cf9926bSpatrick a pair of ``--start-group`` and ``--end-group``. This is especially common 94*1cf9926bSpatrick among system libraries (e.g. ``-lc __isnanl references -lm``, ``-lc 95*1cf9926bSpatrick _IO_funlockfile references -lpthread``, ``-lc __gcc_personality_v0 references 96*1cf9926bSpatrick -lgcc_eh``, and ``-lpthread _Unwind_GetCFA references -lunwind``). 97*1cf9926bSpatrick 98*1cf9926bSpatrick In C++, this is likely an ODR violation. We probably need a dedicated option 99*1cf9926bSpatrick for ODR detection. 100