如何从通用功能符号中计算或指定COFF符号表的“值”?

问题描述 投票:0回答:1

我正在开发类似汇编程序的nasm,现在我正在编写一个生成COFF格式文件的过程。

我的问题非常具体,我受到了

value
上名为
COFF Symbol Table
的字段的困扰。我找不到计算或指定它的方法。

我阅读了文档Microsoft 可移植可执行文件和通用对象文件格式规范。以下是我参考的一篇文章:

The Symbol Table described in this section is inherited from the traditional COFF format.
It is distinct from CodeView® information. A file may contain both a COFF Symbol
Table and CodeView debug information, and the two are kept separate. Some Microsoft
tools use the Symbol Table for limited but important purposes, such as communicating
COMDAT information to the linker. Section names and file names, as well as code and
data symbols, are listed in the Symbol Table.
The location of the Symbol Table is indicated in the COFF Header.
The Symbol Table is an array of records, each 18 bytes long. Each record is either a
standard or auxiliary symbol-table record. A standard record defines a symbol or name,
and has the following format:

|--------| -----|---------------|-------------------------------------------------------------------|
| Offset | Size | Field         | Description                                                       |
|--------| -----|---------------|-------------------------------------------------------------------|
| 0      | 8    | Name (*)      | Name of the symbol, represented by union of three structures.     |
|        |      |               | An array of eight bytes is used if the name is not more than      |
|        |      |               | eight bytes long. See Section 5.4.1,                              |
|        |      |               | "Symbol Name Representation, " for more information.              |
----------------------------------------------------------------------------------------------------|
| 8      | 4    | Value         | Value associated with the symbol.                                 |
|        |      |               | The interpretation of this field depends on Section Number        |
|        |      |               | and Storage Class. A typical meaning is the relocatable address.  |
----------------------------------------------------------------------------------------------------|
| 12     | 2    | SectionNumber | Signed integer identifying the section, using a one-based index   |
|        |      |               | into the Section Table. Some values have special meaning defined  |
|        |      |               | in "Section Number Values."                                       |
----------------------------------------------------------------------------------------------------|
| 14     | 2    | Type          | representing type. Microsoft tools set this field to 0x20         |
|        |      |               | (function) or 0x0 (not a function). See Section 5.4.3,            |
|        |      |               | "Type Representation," for more information.                      |
----------------------------------------------------------------------------------------------------|
| 16     | 1    | StorageClass  | Enumerated value representing storage class.                      |
|        |      |               | See Section 5.4.4, "Storage Class," for more information.         |
|        |      |               |                                                                   |
----------------------------------------------------------------------------------------------------|
| 17     | 1    | NumberOfAux   | Number of auxiliary symbol table entries that follow this record. |
|        |      | Symbols       |                                                                   |
----------------------------------------------------------------------------------------------------|

我想知道如何指定

Value
字段来定义基本的C函数符号,例如
_test

// define basic C function, I think it will be the symbol like "_test" in COFF files.
void test(int value) { return;}

这个文档说

Value
字段代表
relocatable address
。怎么知道?

c++ nasm coff
1个回答
1
投票

终于明白了

value
字段的含义。

该字段表示距符号表开头的偏移值。 如果一个符号和另一个符号之间存在任何机器代码,则该值应包含这些字节大小。

如果符号表中有两个符号。 COFF 对象应该如下所示;

name: 8byte , "symbol-name"
value: 4byte, 0
section_number: 2byte, 1 == section index (.text)
type: 2byte, 0
storageClass: 1byte,  2
numberOfAuxSymbole: 1byte, 0

name: 8byte , 0x0000000400000000
value: 4byte, offset between next symbol
section_number: 2byte, 1
type: 2byte, 0
storageClass: 1byte,  0x01
numberOfAuxSymbole: 1byte, 0x00

name: 8byte , "next symbol"
value: 4byte, 0
section_number: 2byte, 1 == section index (.text)
type: 2byte, 0
storageClass: 1byte,  2
numberOfAuxSymbole: 1byte, 0
© www.soinside.com 2019 - 2024. All rights reserved.