岁月联盟 - 技术社区 - BBS.SYUE.COM's Archiver

猪猪 发表于 2007-2-12 00:51

纯资源DLL的编写

文章作者:四不象

纯资源 DLL 的编写


--------------------------------------------------------------------------------

作者:[四不象]

  所谓纯资源 DLL 就是指不包含任何可执行代码的动态链接库,也就是说这种 DLL 是无需重定位的,这类 DLL 无论被加载到哪个内存地址都是可以的。
  为了便于说明,先介绍一下 PE 文件的加载。每个 PE 文件头都会定义一个 ImageBase,系统根据这个值作为模块的基地址来加载 PE 文件。一般 EXE 文件的 ImageBase 是“0x00400000”,也就是说,这个PE文件的内存映像是从内存地址“0x00400000”开始的。这个基地址和 PE 文件中的代码有着密切关系,因为编译后的代码对内存的引用都使用硬编码。在源代码中我们是通过变量名来引用数据的,而编译后这个变量的内存地址是固定的,可能是“0x00412345”,可执行代码通过这个内存地址来引用,如果实际加载的基地址不是“0x00400000”而是“0x00500000”的话,那着个变量数据的实际地址就变成了“0x00512345”了,“0x00412345”就不能引用变量的数据了。
PE 文件的结构:
Pe Handle: 342e78
Machine: 14c
    Running on 386+
Number of section: 5
Pe Time: Mon Sep 20 01:05:55 1999
Pointer to Symbol Table: 0
Size of Optional Header: e0
Characteristics: 210e
    EXECUTABLE IMAGE
    TARGET MACHINE IS 32BIT
    DEBUG INFORMATION IN FILE
    DLL
OptionalHead Magic No.: 10b
    Pe File is a Pe32 file
PE LinkVer: 6.0
Size of Pe Code: 5000
Size of Pe Initlized Data: 7000
Size of Pe Uninitlized Data: 0
Entry point of this file: 1b88
Base of code: 1000
Base of data: 6000
ImageBase: 10000000
Section Alignment: 1000
File alignment: 1000
Target operating system version: 4.0
File verson: 0.0
Target subsystem ver: 4.0
Size of Image: d000
SizeOfHeaders: 1000
CheckSum: 0
Subsystem is: 2
    Subsystem: windows GUI
Dll Charasteristics: 0
Size Of Stack reserve: 100000
Size of Stack commit: 1000
Size of Heap reserved: 100000
Size of Heap commit: 1000
Number of Pe Data directories : 10
    Export table: 6ae0 3fc
    Import table: 6618 28
    Resource Table: b000 3d0
    Exeception Table: 0 0
    Ceterfy table: 0 0
    Base Relocation table: c000 52c
    Debug Table: 0 0
Architecture: 0 0
    GlobalPrt table: 0 0
    TLS Table: 0 0
    LOAD CONFIG table: 0 0
    Bound import table: 0 0
    IAT: 6000 dc
    Delay Import Descriptor: 0 0
    COM+ Runtime header: 0 0
Section name: .text
    NumberOfLineNumbers: 0
    NumberOfRelocations: 0
    Pointer to LineNumber: 0
    Pointer to RawData: 1000
    Pointer to relocations: 0
    Section Flags: 60000020
    SizeOFRawData: 5000
    VirtualAddress: 1000
    VirtualSize: 49fa
Section name: .rdata
    NumberOfLineNumbers: 0
    NumberOfRelocations: 0
    Pointer to LineNumber: 0
    Pointer to RawData: 6000
    Pointer to relocations: 0
    Section Flags: 40000040
    SizeOFRawData: 1000
    VirtualAddress: 6000
    VirtualSize: edc
Section name: .data
    NumberOfLineNumbers: 0
    NumberOfRelocations: 0
    Pointer to LineNumber: 0
    Pointer to RawData: 7000
    Pointer to relocations: 0
    Section Flags: c0000040
    SizeOFRawData: 3000
    VirtualAddress: 7000
    VirtualSize: 3100
Section name: .rsrc
    NumberOfLineNumbers: 0
    NumberOfRelocations: 0
    Pointer to LineNumber: 0
    Pointer to RawData: a000
    Pointer to relocations: 0
    Section Flags: 40000040
    SizeOFRawData: 1000
    VirtualAddress: b000
    VirtualSize: 3d0
Section name: .reloc
    NumberOfLineNumbers: 0
    NumberOfRelocations: 0
    Pointer to LineNumber: 0
    Pointer to RawData: b000
    Pointer to relocations: 0
    Section Flags: 42000040
    SizeOFRawData: 1000
    VirtualAddress: c000
    VirtualSize: c1c
  一般情况下,EXE 文件是不会碰到这种情况的,它总能被系统加载到指定的地址,但 DLL 文件就不同了。由于 DLL 文件是可被动态加载的,所以不能保证 ImageBase 指向的地址没有被其他的 DLL 占用或已被使用。所以 DLL 文件都会有一个“.reloc”节,这个节存放了用于 DLL 文件重定位的信息。重定位对系统来说是一个很大的开销,所以我们要尽可能得避免重定位的发生。在用 VC 编译 DLL 文件时,可以使用“/fixed”来链接,这样编译后的 DLL 文件就不会存在“.reloc”节了,编译后的文件大小也会被缩小很多(具体缩减大小和 PE 文件的“alignment”有关)。
  使用“/fixed”开关确实很有效,但万一 DLL 无法加载到首选基地址,那该 DLL 就无法被加载。这的确是个问题。对 windows 2000 来说,这个问题很容易解决,在 VC 编译器中,你可以使用“/subsystem:windows,5.0”或“/subsystem:console,5.0”开关来创建一个不包含“.reloc”节的 DLL 文件而不用设置“/fixed”开关。

  在源文件中加入如下代码:
   #pragma comment(linker,"/subsystem:windows,5.0")

翅膀 发表于 2007-10-1 00:08

保存起来好好看看

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.