Skip to content

[BUG] KeyError: 'command' in test script #39

@Merlinus-Ambrosius

Description

@Merlinus-Ambrosius

Bug描述

遇到KeyError: 'command',无法生成program,难以调错。

复现步骤

  1. 打开vscode,点击左下角,Connect to wsl, 进入wsl2();进入本地仓库目录。
  2. make test。无法生成program。
  3. make test_2,同样无法生成program。

期望行为

至少应该生成program供调试。

实际行为

  1. 所有与ld.cpp有关的test case,触发相同的报错:Error: 'command'。
  2. 所有/tests/cases/case_name/build内没有program。
  3. 以test_2为例:
Image

操作系统

Windows + WSL2

编译器版本

g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0

Python版本

Python 3.10.12

相关代码/日志

1. `make test`:

Checking compiler configuration...
Current compiler: g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Compiler supports C++17 ✓
Compiler check completed
------------------------
g++ -std=c++17 -Wall -Wextra -I./include -fPIE -O2 -c -o src/student/ld.o src/student/ld.cpp
g++ -std=c++17 -Wall -Wextra -I./include -fPIE -O2 -o fle_base src/base/cc.o src/base/disasm.o src/base/main.o src/base/exec.o src/base/objdump.o src/base/readfle.o src/student/nm.o src/student/wiang.o src/student/ld.o -pie
python3 grader.py
  Running setup steps [2/2]: Preparing minilibc...

Running 12 test cases...

✓ nm Tool Test [2/2]: Passed
✗ Single File Test [3/3]: Failed

Test 'Single File Test' failed at step 2:
'command'
Traceback (most recent call last):
  File "/home/merlin/programming/linklab/linklab-2024-Merlinus-Ambrosius/grader.py", line 459, in run_test
    self.console.print(f"Command: {error_details['command']}")
KeyError: 'command'

✗ Absolute Addressing Test [4/4]: Failed

Test 'Absolute Addressing Test' failed at step 3:
'command'
Traceback (most recent call last):
  File "/home/merlin/programming/linklab/linklab-2024-Merlinus-Ambrosius/grader.py", line 459, in run_test
    self.console.print(f"Command: {error_details['command']}")
KeyError: 'command'

✗ Absolute + Relative Addressing Test [4/4]: Failed

Test 'Absolute + Relative Addressing Test' failed at step 3:
'command'
Traceback (most recent call last):
  File "/home/merlin/programming/linklab/linklab-2024-Merlinus-Ambrosius/grader.py", line 459, in run_test
    self.console.print(f"Command: {error_details['command']}")
KeyError: 'command'

✗ Position Independent Executable Test [5/5]: Failed

Test 'Position Independent Executable Test' failed at step 4:
'command'
Traceback (most recent call last):
  File "/home/merlin/programming/linklab/linklab-2024-Merlinus-Ambrosius/grader.py", line 459, in run_test
    self.console.print(f"Command: {error_details['command']}")
KeyError: 'command'

✗ Strong Symbol Conflict Test [3/3]: Failed

Test 'Strong Symbol Conflict Test' failed at step 3:
'command'
Traceback (most recent call last):
  File "/home/merlin/programming/linklab/linklab-2024-Merlinus-Ambrosius/grader.py", line 459, in run_test
    self.console.print(f"Command: {error_details['command']}")
KeyError: 'command'

✗ Weak Symbol Override Test [4/4]: Failed

Test 'Weak Symbol Override Test' failed at step 3:
'command'
Traceback (most recent call last):
  File "/home/merlin/programming/linklab/linklab-2024-Merlinus-Ambrosius/grader.py", line 459, in run_test
    self.console.print(f"Command: {error_details['command']}")
KeyError: 'command'

✗ Multiple Weak Symbol Warning [5/5]: Failed

Test 'Multiple Weak Symbol Warning' failed at step 4:
'command'
Traceback (most recent call last):
  File "/home/merlin/programming/linklab/linklab-2024-Merlinus-Ambrosius/grader.py", line 459, in run_test
    self.console.print(f"Command: {error_details['command']}")
KeyError: 'command'

✗ Local Symbol Access Test [7/7]: Failed

Test 'Local Symbol Access Test' failed at step 6:
'command'
Traceback (most recent call last):
  File "/home/merlin/programming/linklab/linklab-2024-Merlinus-Ambrosius/grader.py", line 459, in run_test
    self.console.print(f"Command: {error_details['command']}")
KeyError: 'command'

✗ 64-bit Absolute Relocation Test [3/3]: Failed

Test '64-bit Absolute Relocation Test' failed at step 2:
'command'
Traceback (most recent call last):
  File "/home/merlin/programming/linklab/linklab-2024-Merlinus-Ambrosius/grader.py", line 459, in run_test
    self.console.print(f"Command: {error_details['command']}")
KeyError: 'command'

✗ BSS Section Linking Test [5/5]: Failed

Test 'BSS Section Linking Test' failed at step 4:
'command'
Traceback (most recent call last):
  File "/home/merlin/programming/linklab/linklab-2024-Merlinus-Ambrosius/grader.py", line 459, in run_test
    self.console.print(f"Command: {error_details['command']}")
KeyError: 'command'

✗ Section Permission Control Test [10/10]: Failed

Test 'Section Permission Control Test' failed at step 5:
'command'
Traceback (most recent call last):
  File "/home/merlin/programming/linklab/linklab-2024-Merlinus-Ambrosius/grader.py", line 459, in run_test
    self.console.print(f"Command: {error_details['command']}")
KeyError: 'command'

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┓
┃ Test Case                            ┃ Result ┃  Time ┃     Score ┃ Message             ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━┩
│ nm Tool Test                         │  PASS  │ 0.07s │ 10.0/10.0 │ All steps completed │
│ Single File Test                     │  FAIL  │ 5.04s │  0.0/10.0 │ Error: 'command'    │
│ Absolute Addressing Test             │  FAIL  │ 5.12s │  0.0/10.0 │ Error: 'command'    │
│ Absolute + Relative Addressing Test  │  FAIL  │ 5.18s │  0.0/10.0 │ Error: 'command'    │
│ Position Independent Executable Test │  FAIL  │ 5.32s │  0.0/10.0 │ Error: 'command'    │
│ Strong Symbol Conflict Test          │  FAIL  │ 5.20s │  0.0/10.0 │ Error: 'command'    │
│ Weak Symbol Override Test            │  FAIL  │ 5.22s │  0.0/10.0 │ Error: 'command'    │
│ Multiple Weak Symbol Warning         │  FAIL  │ 5.12s │  0.0/10.0 │ Error: 'command'    │
│ Local Symbol Access Test             │  FAIL  │ 5.19s │  0.0/10.0 │ Error: 'command'    │
│ 64-bit Absolute Relocation Test      │  FAIL  │ 5.12s │  0.0/10.0 │ Error: 'command'    │
│ BSS Section Linking Test             │  FAIL  │ 5.20s │  0.0/10.0 │ Error: 'command'    │
│ Section Permission Control Test      │  FAIL  │ 5.20s │  0.0/10.0 │ Error: 'command'    │
└──────────────────────────────────────┴────────┴───────┴───────────┴─────────────────────┘

╭───────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Total Score: 10.0/120.0 (8.3%)                                                                        │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────╯


To set TEST_BUILD environment variable to the failed test case's build directory:
$ eval "$(python3 grader.py -l)"

2. `make test_2`:

Checking compiler configuration...
Current compiler: g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Compiler supports C++17 ✓
Compiler check completed
------------------------
python3 grader.py --group basic_linking
  Running setup steps [2/2]: Preparing minilibc...

Running 2 test cases in group basic_linking...

✗ Single File Test [3/3]: Failed

Test 'Single File Test' failed at step 2:
'command'
Traceback (most recent call last):
  File "/home/merlin/programming/linklab/linklab-2024-Merlinus-Ambrosius/grader.py", line 459, in run_test
    self.console.print(f"Command: {error_details['command']}")
KeyError: 'command'

✗ Absolute Addressing Test [4/4]: Failed

Test 'Absolute Addressing Test' failed at step 3:
'command'
Traceback (most recent call last):
  File "/home/merlin/programming/linklab/linklab-2024-Merlinus-Ambrosius/grader.py", line 459, in run_test
    self.console.print(f"Command: {error_details['command']}")
KeyError: 'command'

┏━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┓
┃ Test Case                ┃ Result ┃  Time ┃    Score ┃ Message          ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━┩
│ Single File Test         │  FAIL  │ 5.06s │ 0.0/10.0 │ Error: 'command'
│ Absolute Addressing Test │  FAIL  │ 5.08s │ 0.0/10.0 │ Error: 'command'
└──────────────────────────┴────────┴───────┴──────────┴──────────────────┘

╭───────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Total Score: 0.0/20.0 (0.0%)                                                                          │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────╯


To set TEST_BUILD environment variable to the failed test case's build directory:
$ eval "$(python3 grader.py -l)"

补充信息

  1. 我选择在最开始进行分段合并,而非形成.load再拆分。我选择将.text....等段合并入.text段。

  2. 最初我在任务二时未能看到任务六要求内存对齐,当时的报错信息已无法找到。询问同学后得知不对齐就无法通过。

  3. 在更改返回文件的phdrs中每段的size和addr后,我应该实现了对齐,但仍然报错KeyError: 'command'。询问同学后得知,对齐似乎需要对返回文件每段的data进行补0,只改变段头表和程序头表似乎不够。

  4. 我写了一个函数void debug_(FLEObject& aout),用于在返回前将返回文件的所有信息输入一个文件。使用make test_2编译程序,能完成此任务。用“同学成功通过测试的代码”和“我的代码”分别进行make test_2,运行此函数后得到信息。两份信息比对,区别为:

    1. 同学的程序完成了data段补0,而我的data段没有补0,但内容一致。

    2. 以下为对两个代码make test_2后,得到的两份文件:

      • my wrong one:

        !!DEBUG INFO:
        
        Entry: 4194304
        
        Name: a.out
        
        Type: .exe
        
        Program headers vector(phdrs): 
        No.1:
            flags: 5
            name: .text
            size: 4096
            vaddr: 0x400000
        
        Sections: 
        No.1:
            name: .text
            if has symbols(bool): 1
            data: (0x) f3 0f 1e fa 55 48 89 e5 bf 64 00 00 00 b8 3c 00 00 00 0f 05 90 5d c3 
            reloc: 
        
        Section headers vector(shdrs): 
        No.1
            addr: 0x400000
            name: .text
            flags: 5
            offset: 0x0
            size: 0x1000
            type: 1
        
        Symbols: 
        No.1
            name: _start
            offset: 0x400000
            section name: .text
            size: 0x17
            type: GLOBAL
        
      • right one:

        !!DEBUG INFO:
        
        Entry: 4194304
        
        Name: a.out
        
        Type: .exe
        
        Program headers vector(phdrs): 
        No.1:
            flags: 5
            name: .text
            size: 4096
            vaddr: 0x400000
        
        Sections: 
        No.1:
            name: .text
            if has symbols(bool): 0
            data: (0x) f3 0f 1e fa 55 48 89 e5 bf 64 00 00 00 b8 3c 00 00 00 0f 05 90 5d c3 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
            reloc: 
        
        Section headers vector(shdrs): 
        
        Symbols: 
        
  5. KeyError: 'command'产生所需运行时间长达5s。我在函数运行的每个部分添加打印报错信息的语句(如:

    std::cerr << "Now we are out of relocation." << std::endl)。

    我发现make test并不会显示此类信息。运行

    make test_2
    eval "$(python3 grader.py -l)"
    ./ld${TEST_BUILD}/main.fle  -o ${TEST_BUILD}/out.fle

    后,此类信息得以显示,发现程序停在了return处。(即,return前所有行为均执行,但return会让程序保持运行而无任何结果。)

  6. 在return前添加throw std::runtime_error("Not return.")可以快速报错:

    Step 2 'Link program' failed: Required file'${build_dir}/program' not found 

    运行

    make test_2
    eval "$(python3 grader.py -l)"
    ./ld${TEST_BUILD}/main.fle  -o ${TEST_BUILD}/out.fle

    后,程序停止在最后的报错信息,说明确实是return后的未知行为导致程序运行超时且报错信息为KeyError: 'command'

  7. void debug_(FLEObject& aout)

    #include "fle.hpp"
    #include <iomanip>
    #include <iostream>
    std::string toString(RelocationType type)
    {
        switch (type) {
        case RelocationType::R_X86_64_32:
            return "R_X86_64_32";
        case RelocationType::R_X86_64_PC32:
            return "R_X86_64_PC32";
        case RelocationType::R_X86_64_64:
            return "R_X86_64_64";
        case RelocationType::R_X86_64_32S:
            return "R_X86_64_32S";
        default:
            return "Unknown";
        }
    }
    std::string toString2(SymbolType type)
    {
        switch (type) {
        case SymbolType::LOCAL:
            return "LOCAL";
        case SymbolType::GLOBAL:
            return "GLOBAL";
        case SymbolType::UNDEFINED:
            return "UNDEFINED";
        case SymbolType::WEAK:
            return "WEAK";
        default:
            return "Unknown";
        }
    }
    
    #include <fstream>
    void debug_(FLEObject& aout)
    {
        std::ofstream outFile("aout.txt"); // 打开文件用于写入
        if (!outFile.is_open()) {
            std::cerr << "无法打开文件 aout.txt" << std::endl;
            return;
        }
    
        outFile << "!!DEBUG INFO:\n"
                << "\nEntry: " << aout.entry << "\n"
                << "\nName: " << aout.name << "\n"
                << "\nType: " << aout.type << "\n"
                << "\nProgram headers vector(phdrs): " << std::endl;
        int i = 0;
        for (auto& phdr : aout.phdrs) {
            outFile << "No." << ++i << ":\n"
                    << "    flags: " << phdr.flags << "\n"
                    << "    name: " << phdr.name << "\n"
                    << "    size: " << phdr.size << "\n"
                    << "    vaddr: 0x" << std::hex << phdr.vaddr << std::endl;
        }
        outFile << "\nSections: " << std::endl;
        i = 0;
        for (auto& [name, sec] : aout.sections) {
            outFile << "No." << ++i << ":\n"
                    << "    name: " + name + "\n"
                    << "    if has symbols(bool): " << sec.has_symbols << "\n"
                    << "    data: (0x) ";
            for (auto& byte : sec.data)
                outFile << std::setw(2) << std::setfill('0') << (int)byte << " ";
            outFile << std::endl;
            outFile << "    reloc: \n";
    
            for (auto& reloc : sec.relocs) {
                outFile << "        symbol name: " << reloc.symbol << "\n"
                        << "        type: " << (uint32_t)reloc.type << " " << toString(reloc.type) << "\n"
                        << "        offset: 0x" << reloc.offset << "\n        addend: 0x" << reloc.addend << std::endl;
            }
        }
        outFile << "\nSection headers vector(shdrs): \n";
        i = 0;
        for (auto& shdr : aout.shdrs) {
            outFile << "No." << ++i
                    << "\n    addr: 0x" << shdr.addr << "\n    name: " << shdr.name
                    << "\n    flags: " << shdr.flags << "\n    offset: 0x" << shdr.offset
                    << "\n    size: 0x" << shdr.size << "\n    type: " << shdr.type << std::endl;
        }
        outFile << "\nSymbols: \n";
        i = 0;
        for (auto& symbol : aout.symbols) {
            outFile << "No." << ++i
                    << "\n    name: " << symbol.name << "\n    offset: 0x"
                    << symbol.offset << "\n    section name: "
                    << symbol.section << "\n    size: 0x" << symbol.size
                    << "\n    type: " << toString2(symbol.type) << std::endl;
        }
    
        outFile.close(); // 关闭文件
    }
  8. 正在测试将data补0后能否成功形成program。也许这不是一个bug反馈而是一份建议:

    1. 以后的linklab也许可以提前告知完整的程序需要满足的要求,例如对齐要求可以在任务二就提出。
    2. 也许可以尝试说明报错信息的产生原理和可能的产生原因。例如报错Step 2 'Link program' failed: Required file'${build_dir}/program' not found KeyError: 'command',在未生成program的条件下进行调错有点困难,且不知道报错的原理,会不知道如何调错,除非做对照试验。
    3. 在实现链接器的过程中,感觉,如果可以规范设置好功能框架,可能更有助于理解链接器的原理;如果可以,在文档中告知实际链接器的标准原理,这样可以与模拟的链接器做个对比。
      • 因为模拟链接器毕竟和实际链接器不一样,对于相同的功能,实际的链接器是用硬件实现的,而模拟链接器要实现相同功能,完全模拟硬件操作会比较困难。
      • 当然也不一定非要模拟硬件,但这样似乎不利于清晰地掌握链接器的实现原理,可能会带来混淆。例如“符号解析”实际上是在“段合并、布局规划”之前的,而lab里是先进行“段合并”再进行“符号解析”(处理符号冲突、未定义符号情况)。(可能也是我太菜了hh)
      • 感觉,教材上所说的更多是链接器的功能,对于如何实现功能说明得不太清楚。举个例子来说,看书时我不知道什么是符号引用,也就不知道重定位修改的符号引用其实是修改引用该符号时使用的偏移量或地址。我对链接掌握得不算好,在做lab时,本就不清楚的认识似乎变得更不清楚了┭┮﹏┭┮)
  9. 坏消息是,我失败了:补0并不会解决报错问题。┭┮﹏┭┮

  10. 好消息是,发现把symbols和shdrs清空就可以过2-single-file的case了(虽然其他case仍然没过,但这下终于能不报command的了)。

Sub-issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions