posts - 1256, comments - 190, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理



动态装入(Dynamic loading) 指的是当一个进程启动后,将一个可执行的文件(原文是executable,我理解为磁盘上的文件 或者驻留在内存中的例程)或库映射到(或者不常发生的复制)到进程内存空间。动态链接 (dynamic linking)指的是在编译(汇编)之后,分解字符(resolving symbols)——把名字和地址或者偏移量联系起来。这两者难以区分的原因是,大约在进程启动后,编译(汇编)之后这两个过程,通常对二者的微妙区别不加区分地一起完成。大概最清晰的方式来解释这二者的区别就是分别展示二者的各种组合在实践中意味着什么。


可执行的文件拥有一个在编译时生成的 地址/偏移量表,但是实际的代码/数据在进程刚启动时没有装到内存中。这并不是大多数现在的操作系统的处理方式,但是它可能描述了一些老式的overlay systems.如果现在的嵌入式系统也使用这种方式,我一点也不感觉到奇怪。无论是哪种情况,其目的都是给予程序员内存控制的自由同时避免运行时的链接花费。


这通常是在编译时确定动态库的工作方式。可执行文件包含动态/共享库的引用,但是字符表(symbol table)没有或者不完整。装入和链接都在进程启动时进行,被认为是“动态的”链接但不是“动态的”装入。






Dynamic loading refers to mapping (or less often copying) an executable or library into a process's memory after is has started.  Dynamic linking refers to resolving symbols - associating their names with addresses or offsets - after compile time.  The reason it's hard to make a distinction is that the two are often done together without recognizing the subtle distinctions around the parts I put in bold.  Perhaps the clearest way to explain is to go through what the different combinations would mean in practice.

  • Dynamic loading, static linking.  The executable has an address/offset table generated at compile time, but the actual code/data aren't loaded into memory at process start.  This is not the way things tend to work in most systems nowadays, but it would describe some old-fashioned overlay systems.  I'd also be utterly unsurprised if some current embedded systems work this way too.  In either case, the goal is to give the programmer control over memory use while also avoiding the overhead of linking at runtime.
  • Static loading, dynamic linking.  This is how dynamic libraries specified at compile time usually work.  The executable contains a reference to the dynamic/shared library, but the symbol table is missing or incomplete.  Both loading and linking occur at process start, which is considered "dynamic" for linking but not for loading.
  • Dynamic loading, dynamic linking.  This is what happens when you calldlopen or its equivalent on other systems.  The object file is loaded dynamically under program control (i.e. after start), and symbols both in the calling program and in the library are resolved based on the process's possibly-unique memory layout at that time.
  • Static loading, static linking.  Everything is resolved at compile time.  At process start everything is loaded into memory immediately and no extra resolution (linking) is necessary.  In the abstract it's not necessary for the loading to occur from a single file, but I don't think the actual formats or implementations (at least those I'm familiar with) can do multi-file loading without dynamic linking.

【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理