How to use the fixups attribute on a section?

主宰稳场 提交于 2021-02-20 01:36:28

问题


What exactly does "fixups" do when applied on a section? In a fasm sample i found the following section delcaration and i'm really not sure what the fixups attribute does, i couldn't find much information on that in the fasm documentation.

section '.reloc' fixups data readable discardable
if $=$$
    dd 0,8          ; if there are no fixups, generate dummy entry
end if

回答1:


This appears to be a workaround for a bug in how FASM generates PECOFF DLLs. The .reloc section only applies to PECOFF images (EXEs and DLLs), and provides relocations (or "fixups") that allow the image to be loaded at any address. (Relocations of a different sort are used in PECOFF object files; these fixups aren't put in the .reloc section.)

The bug in FASM is that it will generate an empty .reloc section if the DLL doesn't need any relocations rather than not generating one at all. Windows will refuse to load a DLL (or EXE) if has an empty section. The workaround forces a non-empty .reloc section, by adding a dummy "base relocation block" if the .reloc section doesn't have any contents.

Apparently the developer of FASM doesn't think this is a bug in FASM, but rather a bug in Windows, and so hasn't fixed it.

To answer your question directly, the fixups keyword appears to indicate that this section is special to FASM, that it's used for image relocations as described above. Unlike the the other attributes it doesn't correspond to one of the section flags used in PECOFF images, so it appears to only be used internally by FASM.




回答2:


fixups is just another name for relocation entries.
If you are new to relocation on PE, take a look at the official specifications.


Relocation entries tell the loader how to fix (hence the name fixups) the addresses in the compiled code.

The fixups directive tell FASM that the section declared is the one where the relocation entry should be generated (automatically). You can still add your data though, presumably the fixups are written before any user supplied data1.

The test if $=$$ check if the current address counter ($) is equal to the value of the address counter when the section started ($$).
If that is true, the user data will be written at the start of the section, hence no fixups have been generated.

The two dwords dd 0, 8 create an empty entry (a dummy entry).
The second DWORD specify the length of the whole entry including the 8 bytes header, a value of 8 specify no additional data.


I don't know why such dummy entry is created.


1 Just inferring this from the snippet, I don't know for sure.



来源:https://stackoverflow.com/questions/37829732/how-to-use-the-fixups-attribute-on-a-section

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!