是否可以执行差分链接? [英] Is it possible to perform differential linking?

查看:222
本文介绍了是否可以执行差分链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二进制文件,它使用大量具有许多相互依赖关系的对象文件进行链接。每当我重新编译其中一个,我需要链接整个二进制文件。



链接器(特别是GCC或Clang的)支持差分链接 ,其中关于所有其他链接部分之间的相互关系的信息保留足够多,因此当单个部分重新编译时,唯一需要完成的工作是与其他部分的关系+将它们放在二进制文件中?注意:我最感兴趣的是C ++,但是我想这个问题至少会推广到C语言,可能还有其他编译语言。



<在MSVC中,这被称为增量链接。有趣的是,我发现GCC可能会在一定程度上支持这个功能,请尝试使用 -Wl,-i -Wl -r 参数传递给GCC(实际上也应该由CLang支持,因为这些 -Wl 参数只传递给 ld )。






我以前从未使用过,以下makefile:

  OBJS:= ao bo co main.o 

all:test_app

test_app:test_app.reloc
g ++ -o $ @ $ ^

#为增量链接构建一个relocatable对象(-i或-r)
test_app.reloc:$(OBJS)
g ++ -Wl,-i -nostdlib -nostartfiles -o $ @ $ ^
$ b $(OBJS):makefile

%.o:%.cpp
g ++ -c -o $ @ $<

这会构建应用程序,但我不完全确定它在内部的作用,例如MSVC中的增量链接。



特别是,使用-Wl,-i时需要参数-nostdlib,以便默认libs将不会被传递给ld(然后找不到它们 - 没有它,我有错误 / usr / bin / ld:找不到-lgcc_s ) 。




另一个版本可能实际上效果更好(不确定,这需要在更大的应用程序上测试才能看到如果单个对象更新的链接时间有所增加):

  OBJS:= a.ro b.ro c。 ro main.ro 

all:test_app

test_app:$(OBJS)
g ++ -o $ @ $ ^

%。 o:%.cpp
g ++ -c -o $ @ $<

%.ro:%.o
g ++ -Wl,-i -nostdlib -nostartfiles -o $ @ $<




  • 基本上为每个对象创建可重定位文件(这可能是一个重要的部分obj文件链接到可执行文件中),然后更新必要的可重定位文件。对于最后的链接步骤,使用relocatables将所有内容链接在一起(但之前已完成部分链接)。也可以创建组的对象文件,将它们分组到一个可重定位的对象中,这样在最后会有更少的对象文件(不知道是否会在末尾带来任何东西)。


    I have a binary which is linked using a large number of object files with many interdependencies. Whenever I recompile even one of these, I need to link the entire binary.

    Do linkers (specifically, those of GCC or Clang) support some method of "differential linking", in which enough information is kept about the inter-relations between all the other linked parts, so that the only work that needs to be done when a single part is recompiled is its relations to the other parts + putting them together in the binary?

    Note: I'm mostly interested in C++ but I guess this question generalizes at least to C and probably to other compiled languages.

    解决方案

    In MSVC this is called "incremental linking". Interestingly, what I've found that GCC might support that to some extent, try using the "-Wl,-i" or "-Wl,-r" parameters to GCC (should be actually also supported by CLang, as these "-Wl" parameters are just passed to ld).


    I never used it before, but I made this work with the following makefile:

    OBJS := a.o b.o c.o main.o
    
    all:    test_app
    
    test_app:   test_app.reloc
        g++ -o $@ $^
    
    # build a "relocatable" object for incremental linking (either -i or -r)
    test_app.reloc: $(OBJS)
        g++ -Wl,-i -nostdlib -nostartfiles -o $@ $^
    
    $(OBJS):    makefile
    
    %.o:    %.cpp
        g++ -c -o $@ $<
    

    This builds the app, but I'm not entirely sure what it does internally, if it really does something like "incremental linking" done in MSVC.

    In particular, the parameter "-nostdlib" is necessary when using the "-Wl,-i" so that the default libs will not be passed to the ld (which then can't find them - without it I had the error "/usr/bin/ld: cannot find -lgcc_s").


    Another version which might actually work better (not sure, this would need to be tested on a bigger application to see if there is some gain in the link time for single object updates):

    OBJS := a.ro b.ro c.ro main.ro
    
    all:    test_app
    
    test_app:   $(OBJS)
        g++ -o $@ $^
    
    %.o:    %.cpp
        g++ -c -o $@ $<
    
    %.ro:   %.o
        g++ -Wl,-i -nostdlib -nostartfiles -o $@ $<
    

    • Basically creating relocatable file for each object (which might be perhaps a significant portion of the linkage of obj files into the executable) and then just updating the relocatables necessary. For the final link step using the relocatables to link everything together (but part of the linkage has been already done before).

    It is also possible to create "groups" of object files to be grouped in a single relocatable, so that there will be less of them at the end (not sure if that would bring anything at the end though).

    这篇关于是否可以执行差分链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆