How to install 16 bit version of libcc

245 Views Asked by At
$uname -i
x86_64
$ uname -r
4.15.0-041500-generic

I am trying to compile that gets memory map of a system. The OSDev shows some code how that can be done.

The code is as follows:

// 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
        [...]
    }
}

Obviously the code has to be compiled in 16 bit mode. So I used gcc -m16 smapentry.c -o smap This has resulted in a error which says.

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

which means I have a 64 libgcc installed. For this reason, -m64 and -m32 works fine. only -m16 doesn't work. It looks like I need to install libgcc version to compile 16 bit code. Which library should I use?

0

There are 0 best solutions below