linker.ld 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. ENTRY(_start)
  2. SECTIONS
  3. {
  4. /*
  5. We could also pass the -Ttext 0x7C00 to as instead of doing this.
  6. If your program does not have any memory accesses, you can omit this.
  7. */
  8. . = 0x7c00;
  9. .text :
  10. {
  11. __start = .;
  12. /*
  13. We are going to stuff everything
  14. into a text segment for now, including data.
  15. This does not matter here because we are generating OUR kernel,
  16. so Linux will not be there to prevent us from writing to a data section.
  17. */
  18. *(.bootstrap)
  19. *(*)
  20. /*
  21. Magic bytes. 0x1FE == 510.
  22. We could add this on each Gas file separately with `.word`,
  23. but this is the perfect place to DRY that out.
  24. */
  25. . = 0x1FE;
  26. SHORT(0xAA55)
  27. /*
  28. This is only needed if we are going to use a 2 stage boot process,
  29. e.g. by reading more disk than the default 512 bytes with BIOS `int 0x13`.
  30. */
  31. *(.stage2)
  32. /*
  33. Number of sectors in stage 2. Used by the `int 13` to load it from disk.
  34. The value gets put into memory as the very last thing
  35. in the `.stage` section if it exists.
  36. We must put it *before* the final `. = ALIGN(512)`,
  37. or else it would fall out of the loaded memory.
  38. This must be absolute, or else it would get converted
  39. to the actual address relative to this section (7c00 + ...)
  40. and linking would fail with "Relocation truncated to fit"
  41. because we are trying to put that into al for the int 13.
  42. */
  43. __stage2_nsectors = ABSOLUTE((. - __start) / 512);
  44. /* Ensure that the generated image is a multiple of 512 bytes long. */
  45. . = ALIGN(512);
  46. __end = .;
  47. __end_align_4k = ALIGN(4k);
  48. }
  49. }
  50. /*
  51. The linux kernel 4.2 uses linker scripts like:
  52. - https://github.com/torvalds/linux/blob/v4.2/arch/x86/boot/setup.ld
  53. The kernel also uses the `.lds` extension for its scripts.
  54. */