The Place to Start for Operating System Developers
It is currently Sat Jul 20, 2019 7:56 am

All times are UTC - 6 hours

Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Error compile GDT and Interrupt
PostPosted: Mon Jul 01, 2019 11:04 am 

Joined: Mon Jul 01, 2019 10:46 am
Posts: 3
Good evening all. Now, I am learning OS Development from James Molloy. But I've got problem here.
Error like this
ld -mi386pe -nostdlib --nmagic -T linker.ld -o kernel.pe loader.o kernel.o common.o monitor.o descriptor_table.o gdt.o isr.o interrupt.o
descriptor_table.o: In function `init_gdt':
/cygdrive/d/AnantaOS/src/descriptor_table.c:75: undefined reference to `gdt_flush'
interrupt.o:fake:(.text+0x150): undefined reference to `isr_handler'
interrupt.o:fake:(.text+0x6): undefined reference to `isr_common'
interrupt.o:fake:(.text+0x10): undefined reference to `isr_common'
interrupt.o:fake:(.text+0x1a): undefined reference to `isr_common'
interrupt.o:fake:(.text+0x24): undefined reference to `isr_common'
interrupt.o:fake:(.text+0x2e): undefined reference to `isr_common'
interrupt.o:fake:(.text+0x38): more undefined references to `isr_common' follow
make: *** [Makefile:14: kernel.pe] Error 1

I'm sorry, I don't using cross compiler (You know this is really bad habbit for OSDev). I'm using cygwin in Windows 10 for OSDevelopment. But I need help alot. I want continue this lesson more depth (I still newbie).

code in gdt.s
.section .text
.align 4

.global gdt_flush

    mov 4(%esp),%eax
    lgdt (%eax)
    mov $0x10,%ax
    mov %ax,%ds
    mov %ax,%es
    mov %ax,%fs
    mov %ax,%ss
    mov %ax,%gs
    jmp $0x08, $.flush


.global idt_flush

   mov 4(%esp),%eax
   lidt (%eax)

Interrupt code
.section .text
.align 4

.macro ISR_NOERR index
    .global _isr\index
        push $0
        push $\index
        jmp isr_common

.macro ISR_ERR index
    .global _isr\index
        push $\index
        jmp isr_common

/* Standard X86 interrupt service routines */
ISR_ERR   10
ISR_ERR   11
ISR_ERR   12
ISR_ERR   13
ISR_ERR   14

.extern isr_handler

    mov %ds,%ax
    push %eax
    mov $0x10, %ax
    mov %ax, %ds
    mov %ax, %es
    mov %ax, %fs
    mov %ax, %gs

    call isr_handler

    pop %eax
    mov %ax, %ds
    mov %ax, %es
    mov %ax, %fs
    mov %ax, %gs

    add $8, %esp

 Post subject: Re: Error compile GDT and Interrupt
PostPosted: Mon Jul 01, 2019 11:12 am 

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 440
Windows 32-bit object file formats require externals to have a leading underscore. gdt_flush should be _gdt_flush in the assembly code. This is actually a side effect of not using a cross compiler and the types of things you have to concern yourself with when using a native toolchain for the host environment. If you did choose to use a cross compiler (which you have decided not to) you wouldn't have to concern yourself with this issue.

 Post subject: Re: Error compile GDT and Interrupt
PostPosted: Mon Jul 01, 2019 9:31 pm 

Joined: Mon Jul 01, 2019 10:46 am
Posts: 3
Thank you it works. But new problem arrise causing virtualbox meditation (error hang). now, i can list the code what i got from james molloy.

#include "common.h"
#include "descriptor_table.h"

struct gdt_entry_struct gdt_entries[5];
struct gdt_ptr_struct gdt_ptr;
struct idt_ptr_struct idt_ptr;
struct idt_entry_struct idt_entries[256];

extern void idt_flush(uint32_t);
extern void gdt_flush(uint32_t);

static void init_gdt();
static void init_idt();
static void gdt_set_gate(int32_t,uint32_t,uint32_t,uint8_t,uint8_t);
static void idt_set_gate(uint8_t,uint32_t,uint16_t,uint8_t);

void init_descriptor_tables() {

static void init_gdt() {
    gdt_ptr.limit = (sizeof(struct gdt_entry_struct) * 5) - 1;
    gdt_ptr.base = (uint32_t)&gdt_entries;

static void gdt_set_gate(int32_t num,uint32_t base,uint32_t limit,uint8_t access,uint8_t granularity) {
    gdt_entries[num].base_low = (base & 0xFFFF);
    gdt_entries[num].base_middle = (base >> 16) & 0xFFFF;
    gdt_entries[num].base_high = (base >> 24) & 0xFFFF;
    gdt_entries[num].limit_low = (limit & 0xFFFF);
    gdt_entries[num].granularity = (limit >> 16) & 0x0F;
    gdt_entries[num].granularity |= granularity & 0x0F;
    gdt_entries[num].access = access;

static void init_idt() {
    idt_ptr.limit = sizeof(struct idt_entry_struct) * 256 - 1;
    idt_ptr.base = (uint32_t)&idt_entries;
    memset(&idt_entries,0,sizeof(struct idt_entry_struct) * 256);
    idt_set_gate( 0, (uint32_t)isr0 , 0x08, 0x8E);
    idt_set_gate( 1, (uint32_t)isr1 , 0x08, 0x8E);
    idt_set_gate( 2, (uint32_t)isr2 , 0x08, 0x8E);
    idt_set_gate( 3, (uint32_t)isr3 , 0x08, 0x8E);
    idt_set_gate( 4, (uint32_t)isr4 , 0x08, 0x8E);
    idt_set_gate( 5, (uint32_t)isr5 , 0x08, 0x8E);
    idt_set_gate( 6, (uint32_t)isr6 , 0x08, 0x8E);
    idt_set_gate( 7, (uint32_t)isr7 , 0x08, 0x8E);
    idt_set_gate( 8, (uint32_t)isr8 , 0x08, 0x8E);
    idt_set_gate( 9, (uint32_t)isr9 , 0x08, 0x8E);
    idt_set_gate(10, (uint32_t)isr10, 0x08, 0x8E);
    idt_set_gate(11, (uint32_t)isr11, 0x08, 0x8E);
    idt_set_gate(12, (uint32_t)isr12, 0x08, 0x8E);
    idt_set_gate(13, (uint32_t)isr13, 0x08, 0x8E);
    idt_set_gate(14, (uint32_t)isr14, 0x08, 0x8E);
    idt_set_gate(15, (uint32_t)isr15, 0x08, 0x8E);
    idt_set_gate(16, (uint32_t)isr16, 0x08, 0x8E);
    idt_set_gate(17, (uint32_t)isr17, 0x08, 0x8E);
    idt_set_gate(18, (uint32_t)isr18, 0x08, 0x8E);
    idt_set_gate(19, (uint32_t)isr19, 0x08, 0x8E);
    idt_set_gate(20, (uint32_t)isr20, 0x08, 0x8E);
    idt_set_gate(21, (uint32_t)isr21, 0x08, 0x8E);
    idt_set_gate(22, (uint32_t)isr22, 0x08, 0x8E);
    idt_set_gate(23, (uint32_t)isr23, 0x08, 0x8E);
    idt_set_gate(24, (uint32_t)isr24, 0x08, 0x8E);
    idt_set_gate(25, (uint32_t)isr25, 0x08, 0x8E);
    idt_set_gate(26, (uint32_t)isr26, 0x08, 0x8E);
    idt_set_gate(27, (uint32_t)isr27, 0x08, 0x8E);
    idt_set_gate(28, (uint32_t)isr28, 0x08, 0x8E);
    idt_set_gate(29, (uint32_t)isr29, 0x08, 0x8E);
    idt_set_gate(30, (uint32_t)isr30, 0x08, 0x8E);
    idt_set_gate(31, (uint32_t)isr31, 0x08, 0x8E);

static void idt_set_gate(uint8_t num,uint32_t base,uint16_t sel,uint8_t flags) {
    idt_entries[num].base_low = base & 0xFFFF;
    idt_entries[num].base_high = (base >> 16) & 0xFFFF;
    idt_entries[num].selector = sel;
    idt_entries[num].always_zero = 0;
    idt_entries[num].flags = flags | 0x60;

#include <stdint.h>
struct gdt_entry_struct{
    uint16_t limit_low;
    uint16_t base_low;
    uint8_t base_middle;
    uint8_t access;
    uint8_t granularity;
    uint8_t base_high;
} __attribute__((packed));

struct gdt_ptr_struct {
    uint16_t limit;
    uint32_t base;
} __attribute__((packed));

struct idt_entry_struct {
    uint16_t base_low;
    uint16_t selector;
    uint8_t always_zero;
    uint8_t flags;
    uint16_t base_high;
} __attribute__((packed));

struct idt_ptr_struct {
   uint16_t limit;
   uint32_t base;
} __attribute__((packed));

void init_descriptor_tables();

extern void isr0();
extern void isr1();
extern void isr2();
extern void isr3();
extern void isr4();
extern void isr5();
extern void isr6();
extern void isr7();
extern void isr8();
extern void isr9();
extern void isr10();
extern void isr11();
extern void isr12();
extern void isr13();
extern void isr14();
extern void isr15();
extern void isr16();
extern void isr17();
extern void isr18();
extern void isr19();
extern void isr20();
extern void isr21();
extern void isr22();
extern void isr23();
extern void isr24();
extern void isr25();
extern void isr26();
extern void isr27();
extern void isr28();
extern void isr29();
extern void isr30();
extern void isr31();

 Post subject: Re: Error compile GDT and Interrupt
PostPosted: Tue Jul 02, 2019 8:36 am 
User avatar

Joined: Fri Oct 27, 2006 9:42 am
Posts: 1461
Location: Athens, GA, USA
I try to get this out to all new members, to get everyone started on the same footing. I usually send it by email, but I think I will take this opportunity to repost it in the forum again. I hope this helps.

The first thing I want to say is this: if you aren't already using version control for all software projects you are working on, drop everything and start to do that now. Set up a VCS such as Git, Subversion, Mercurial, Bazaar, or what have you - which you use is less important than the fact that you need to use it. Similarly, setting up your repos on an offsite host such as Gitlab, Github, Sourceforge, CloudForge, or BitBucket should be the very first thing you do whenever you start a new project, no matter how large or small it is.

If nothing else, it makes it easy to share your code with us on the forum, as you can just post a link, rather than pasting oodles and oodles of code into a post.

Once you have that out of the way (if you didn't already), you can start to consider the OS specific issues.

If you haven't already, I would strongly advise you to read the introductory material in the wiki:

After this, go through the material on the practical aspects of
running an OS-dev project:

I strongly suggest that you read through these pages in detail, along with the appropriate ones to follow, before doing any actual development. These pages should ensure that you have at least the basic groundwork for learning about OS dev covered.

This brings you to your first big decision: which platform, or platforms, to target. Commonly options include:
  • x86 - the CPU architecture of the stock PC desktops and laptops, and the system which remains the 'default' for OS dev on this group. However, it is notoriously quirky, especially regarding Memory Segmentation, and the sharp divisions between 16-bit Real Mode, 16-bit and 32-bit Protected Modes, and 64-bit Long Mode.
  • ARM - a RISC architecture widely used on mobile devices and for 'Internet of Things' and 'Maker' equipment, including the popular Raspberry Pi and Beagleboard single board computers. While it is generally seen as easier to work with that x86, most notably in the much less severe differences in between the 32-bit and 64-bit modes and the lack of memory segmentation, the wiki and other resources don't cover it nearly as well (though this is changing over time as it becomes more commonly targeted).
  • MIPS, another RISC design which is slightly older than ARM. It is one of the first RISC design to come out, being part of the reason the idea caught on, and is even simpler than ARM in terms of programming, though a bit tedious when it comes to assembly programming. While it was widely used in workstations and game consoles in the 1990s, it has declined significantly due to mismanagement by the owners of the design, and is mostly seen in devices such as routers. There are a handful of System on Chip single-board computers that use it, such as the Creator Board and the Onion Omega2, and manufacturers in both China and Russia have licensed the ISA with the idea of breaking their dependence on Intel. The ISA was made open-source in 2018, which may lead to more widespread use. Finding good information on the instruction set is easy, as it is widely used in courses on assembly language and computer architecture and there are several emulators that run MIPS code, but finding usable information on the actual hardware systems using it is often difficult at best.
  • RISC-V is an up and coming open source hardware ISA, closely related to MIPS in overall design, but so far is Not Ready For Prime Time. It is developing rapidly, however, and a handful of maker-grade SBCs using it have come on the market in 2018 and 2019.

You then need to decide which Languages to use for the kernel. For most OS-Developers this means knowing and using C; while other languages can be used, it is important to know how to read C code, even if you don't use C, as most OS examples are written in it. You will also need to know at least some assembly language for the target platform, as there are always parts of the kernel and the device drivers which cannot be done in high-level languages.

You further need to choose the compiler, assembler, linker, build tool, and support utilities to use - what is called the 'toolchain' for your OS. For most platforms, there aren't many to choose from, and the obvious choice would be GCC and the Binutils toolchain due to their ubiquity. However, on the Intel x86 platform, it isn't as simple, as there are several other toolchains which are in widespread use for it, the most notable being the Microsoft one - a very familiar one to Windows programmers, but one which presents problems in OSDev. The biggest issue with Visual Studio, and with proprietary toolchains in general, is that using it rules out the possibility of your OS being "self-hosting" - that is to say, being able to develop your OS in the OS itself, something most OSdevs do want to eventually be able to do. The fact that Porting GCC to your OS is feasible, whereas porting proprietary x86 toolchains isn't, is a big factor in the use Binutils and GCC, as it their deep connection to Linux and other Unix derivatives.

Regardless of the high-level language you use for OS dev (if any), you will still need to use assembly language, which means choosing an assembler. If you are using Binutils and GCC, the obvious choice would be GAS, but for x86 especially, there are other assemblers which many OSdevs prefer, such as Netwide Assembler (NASM) and Flat Assembler (FASM).

The important thing here is that assembly language syntax varies more among the x86 assemblers than it does for most other platforms, with the biggest difference being that between the Intel syntax used in the majority of x86 assemblers, and the AT&T syntax used in GAS. You can see an overview of the differences on the somewhat misnamed wiki page Opcode syntax. While it is possible to coax GAS to use the Intel syntax using the .intel_syntax noprefix directive, the opposite is generally not true for Intel-based assemblers such as NASM, and even with that directive, GAS is still quite different from other x86 assemblers in other regards.

It is still important to understand that the various Intel syntax assemblers - NASM, FASM, and YASM among others - have differences in how they handle indexing, in the directives they use, and in their support for features such as macros and defining data structures. While most of these follow the general syntax of Microsoft Assembler (MASM), they all diverge from it in various ways.

Once you know which platform you are targeting, and the toolchain you want to use, you need to understand them. You should read up on the core technologies for the platform. Assuming that you are targeting the PC architecture, this would include:

This leads to the next big decision: which Bootloader to use. There are a number of different standard bootloaders for x86, with the most prominent being GRUB. We strong recommend against Rolling Your Own Bootloader, but it is an option as well.

You need to consider what kind of File System to use. Common ones used when starting out in OS dev include:

We generally don't recommend designing your own, but as with boot loaders, it is a possibility as well.

While this is a lot of reading, it simply reflects the due diligence that any OS-devver needs to go through in order to get anywhere. OS development, even as a simple project, is not amenable to the Stack Overflow cut-and-paste model of software development; you really need to understand a fair amount of the concepts and principles before writing any code, and the examples given in tutorials and forum posts generally are exactly that. Copying an existing code snippet without at least a basic idea of what it is doing simply won't do. While learning itself is an iterative process - you learn one thing, try it out, see what worked and what didn't, read some more, etc. - in this case a basic foundation is needed at the start. Without a solid understanding of at least some of the core ideas before starting, you simply can't get very far in OS dev.

Hopefully, this won't scare you off; it isn't nearly as bad as it sounds. It just takes a lot of patience and a bit of effort, a little at a time.

Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
μή εἶναι βασιλικήν ἀτραπόν ἐπί γεωμετρίαν
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.

 Post subject: Re: Error compile GDT and Interrupt
PostPosted: Tue Jul 02, 2019 1:11 pm 
User avatar

Joined: Wed Dec 12, 2018 12:16 pm
Posts: 120
Location: Chile
Another recomendation: You're in Windows 10, so instead of using Cygwin, is better to use WSL (Windows Subsystem for Linux). I use it, and no problems so far.

 Post subject: Re: Error compile GDT and Interrupt
PostPosted: Fri Jul 05, 2019 8:06 am 

Joined: Mon Jul 01, 2019 10:46 am
Posts: 3
How about vagrant, can i use it?
Thank you Schol-R-LEA. Im trying your advice.

 Post subject: Re: Error compile GDT and Interrupt
PostPosted: Fri Jul 05, 2019 11:04 am 
User avatar

Joined: Tue Oct 17, 2006 10:35 pm
Posts: 37
Location: Eagle, ID (USA)
Yes, you can use vagrant to setup a linux box and compile your own operating system just like you can with a docker image or a virtual machine with linux on it. While you wont have the issue with underscores on vagrant like you did with using cygwin since the file format will be elf and not pe. It is still recommended to setup a cross-compiler since the compiler on linux is compiled with more options than you want and you will need to pass some additional options to your makefile to make it work.

"I think it may be time for some guru meditation"
"Barbarians don't do advanced wizardry"

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 7 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group