Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions src/student/nm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,39 @@

void FLE_nm(const FLEObject& obj)
{
// TODO: 实现符号表显示工具
// 1. 遍历所有符号
// - 跳过未定义符号 (section为空的情况)
// - 使用16进制格式显示符号地址
// 遍历符号表
for (const auto& symbol : obj.symbols) {
// 跳过未定义符号
if (symbol.section.empty()) {
continue; // 符号没有 section,表示是未定义符号
}

// 2. 确定符号类型字符
// - 处理弱符号: 代码段用'W',其他段用'V'
// - 根据段类型(.text/.data/.bss/.rodata)和符号类型(GLOBAL/LOCAL)确定显示字符
// - 全局符号用大写字母,局部符号用小写字母
// 确定符号类型和输出格式
std::string type;
if (symbol.type == SymbolType::WEAK) {
type = (symbol.section == ".text") ? "W" : "V";
} else if (symbol.type == SymbolType::GLOBAL) {
if (symbol.section == ".text") {
type = "T"; // 全局函数符号
} else if (symbol.section == ".data") {
type = "D"; // 全局数据符号
} else if (symbol.section == ".bss") {
type = "B"; // 全局未初始化数据符号
}
} else if (symbol.type == SymbolType::LOCAL) {
if (symbol.section == ".text") {
type = "t"; // 局部函数符号
} else if (symbol.section == ".data") {
type = "d"; // 局部数据符号
} else if (symbol.section == ".bss") {
type = "b"; // 局部未初始化数据符号
}
}

// 3. 按格式输出
// - [地址] [类型] [符号名]
// - 地址使用16位十六进制,左侧补0

throw std::runtime_error("Not implemented");
}
// 输出符号信息
// 确保地址是16位十六进制,左侧补零
std::cout << std::setw(16) << std::setfill('0') << std::hex << symbol.offset << " "
<< type << " "
<< symbol.name << std::endl;
}
}