0
$uname -i
x86_64
$ uname -r
4.15.0-041500-generic

我正在尝试编译以获取系统的内存映射。OSDev 显示了一些代码如何做到这一点。

代码如下:

// running in real mode may require:
__asm__(".code16gcc\n");

// SMAP entry structure
#include <stdint.h>
typedef struct SMAP_entry {

    uint32_t BaseL; // base address uint64_t
    uint32_t BaseH;
    uint32_t LengthL; // length uint64_t
    uint32_t LengthH;
    uint32_t Type; // entry Type
    uint32_t ACPI; // extended

}__attribute__((packed)) SMAP_entry_t;

// load memory map to buffer - note: regparm(3) avoids stack issues with gcc in real mode
int __attribute__((noinline)) __attribute__((regparm(3))) detectMemory(SMAP_entry_t* buffer, int maxentries)
{
    uint32_t contID = 0;
    int entries = 0, signature, bytes;
    do 
    {
        __asm__ __volatile__ ("int  $0x15" 
                : "=a"(signature), "=c"(bytes), "=b"(contID)
                : "a"(0xE820), "b"(contID), "c"(24), "d"(0x534D4150), "D"(buffer));
        if (signature != 0x534D4150) 
            return -1; // error
        if (bytes > 20 && (buffer->ACPI & 0x0001) == 0)
        {
            // ignore this entry
        }
        else {
            buffer++;
            entries++;
        }
    } 
    while (contID != 0 && entries < maxentries);
    return entries;
}

// in your main routine - memory map is stored in 0000:1000 - 0000:2FFF for example
[...] {
    [...]
    SMAP_entry_t* smap = (SMAP_entry_t*) 0x1000;
    const int smap_size = 0x2000;

    int entry_count = detectMemory(smap, smap_size / sizeof(SMAP_entry_t));

    if (entry_count == -1) {
        // error - halt system and/or show error message
        [...]
    } else {
        // process memory map
        [...]
    }
}

显然代码必须以 16 位模式编译。所以我用 gcc -m16 smapentry.c -o smap了这导致了一个错误,上面写着。

usr/bin/x86_64-linux-gnu-ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/7/libgcc.a when searching for -lgcc
/usr/bin/x86_64-linux-gnu-ld: cannot find -lgcc

这意味着我安装了 64 libgcc。因此,-m64 和 -m32 可以正常工作。只有 -m16 不起作用。看来我需要安装 libgcc 版本来编译 16 位代码。我应该使用哪个库?

4

0 回答 0