porting.txt 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. Some notes to help future porters. Replace 'ARCH' with whatever arch
  2. you are hacking on.
  3. ====================
  4. === Config Files ===
  5. ====================
  6. - create extra/Configs/Config.ARCH
  7. See the other arch files for some good examples. powerpc/sparc/alpha
  8. should be pretty simple templates.
  9. - add ARCH to the 'Target Architecture' list in extra/Configs/Config.in
  10. - Initially you will want to disable shared libraries, since making
  11. the shared library loader work requires you first have basic architecture
  12. support working. Thus you should add ARCH_HAS_NO_SHARED and
  13. ARCH_HAS_NO_LDSO to Config.ARCH's TARGET_ARCH
  14. - When static pie support is added this TARGET_arch can be appended to the
  15. list in extra/Configs/Config.in
  16. ====================
  17. === libc sysdeps ===
  18. ====================
  19. (note: if glibc has already been ported to your arch, you can usually just
  20. copy a lot of files from them rather than coding from scratch)
  21. - create libc/sysdeps/linux/ARCH
  22. - copy Makefile and Makefile.arch from libc/sysdeps/linux/i386/
  23. - set CSRC and SSRC to nothing in Makefile.arch for now
  24. - create crt1.S which defines the _start function ... you will probably want
  25. to clear the frame pointer to make gdb happy, and then you will want to call
  26. the funcion __uClibc_main() which takes these parameters:
  27. __uClibc_main(main(), argc, argv, _init(), _fini())
  28. Initially if you wish to make things easier on yourself, you can disable the
  29. UCLIBC_CTOR_DTOR option and just set the init/fini arguments to NULL.
  30. glibc generally stores this function in libc/sysdeps/ARCH/elf/start.S
  31. - create these additional files in ARCH/bits/
  32. (template versions can be found in common/bits/ for you to tweak)
  33. endian.h fcntl.h setjmp.h stackinfo.h uClibc_arch_features.h wordsize.h
  34. kernel_types.h should be created based upon linux asm-ARCH/posix_types.h
  35. copy linux asm-ARCH/stat.h to bits/kernel_stat.h
  36. create syscalls.h based upon linux's unistd.h / glibc's sysdeps.h ... really
  37. you just want to define the _syscall[0-6] macros. It is important that
  38. these syscalls should be PIC safe (or you should provide a PIC and non-PIC
  39. version) if you wish to properly support shared libraries.
  40. - at this point, you should have enough to generate a working HELLO WORLD
  41. static binary
  42. - if you want UCLIBC_CTOR_DTOR support, you will need to create crti.S and
  43. crtn.S files which define function prologues/epilogues.
  44. - for a more stable static port, you will need to create these files (and
  45. update the Makefile.arch values accordingly)
  46. __longjmp bsd-_setjmp bsd-setjmp brk clone setjmp syscall vfork
  47. usually these are written in assembler, but you may be able to cheat and
  48. write them in C ... see other ports for more information
  49. - Once static and pie executables are stable, static-pie support can be
  50. added by modifying crt1.S to calculate the address that the kernel loaded
  51. the main elf. Once the elf load address is found, call reloc_static_pie to
  52. perform all the dynamic relocations normally handled by ldso. This new
  53. code should be placed at the begining of _start and surrounded by defines
  54. so that it is only compiled into rcrt1.o and not the static or shared
  55. versions. This is usually done by using the special L_rcrt1 preprocessor
  56. define. i386 and x86_64 have good reference implementations.
  57. ====================
  58. === ldso sysdeps ===
  59. ====================
  60. - elf.h - presumably you've already taught binutils all about the random ELF
  61. relocations your arch needs, so now you need to make sure the defines exist
  62. for uClibc. make sure the EM_### define exists and all of the R_###_###
  63. reloc defines.
  64. - enable ldso/shared options in your extra/Configs/Config.ARCH file
  65. - you will need to create the following files in ldso/ldso/ARCH/
  66. dl-startup.h dl-syscalls.h dl-sysdep.h elfinterp.c resolve.S
  67. - dl-startup.h:
  68. - define the _start function which should call _dl_start which takes just one
  69. parameter ... a pointer to argc (usually on the stack)
  70. glibc stores this function in sysdeps/ARCH/dl-machine.h as RTLD_START
  71. - define the GET_ARGV() macro which calculates the value of argv based upon
  72. the parameter passed to _dl_start (usually it's simply just ARGS+1)
  73. - define PERFORM_BOOTSTRAP_RELOC() macro which will handle just the relocs
  74. that the ldso itself will generate
  75. - dl-syscalls.h:
  76. if you wrote your bits/syscalls.h file correctly in the libc step above, you
  77. can simply copy this file from another arch and be done ... otherwise you
  78. will have to define the syscall[0-6] macros again, but this time setting
  79. _dl_errno instead of just errno
  80. - dl-sysdep.h:
  81. misc cruft goes in here ... you want to:
  82. - either define or undefine ELF_USES_RELOCA
  83. - define the INIT_GOT macro
  84. - define MAGIC1 to the EM_### value your ELF arch uses
  85. - define ELF_TARGET to a string name for your arch
  86. - define the do_rem() macro
  87. - define misc ALIGN macro's
  88. - define elf_machine_type_class() macro
  89. - define the inline functions elf_machine_dynamic, elf_machine_load_address,
  90. and elf_machine_relative
  91. glibc stores a bunch of these values in sysdeps/ARCH/dl-machine.h
  92. - elfinterp.c:
  93. define all the relocation functions ... it's best if you just copy from
  94. another arch which uses the same type of relocations (REL or RELA) and
  95. start from there.
  96. - resolve.S:
  97. front end of lazy relocation ... define the _dl_linux_resolve symbol which
  98. is called by a PLT entry which has yet to be setup ... you will want to:
  99. - set up arguments for _dl_linux_resolver()
  100. - call _dl_linux_resolver()
  101. - clean up after call
  102. - jump to function address now stored in PLT
  103. glibc stores this function in sysdeps/ARCH/dl-trampoline.S
  104. - utils/ldd.c - if you want support for ldso cache files (spoiler: you do),
  105. then you'll need to teach ldd a little. generally, the fallback code
  106. should be smart and "just work", but you should be explicit. just pop
  107. it open and add an appropriate ifdef for your arch and set MATCH_MACHINE()
  108. and ELFCLASSM. there are plenty examples and you're (hopefully) smart.
  109. ====================
  110. === Misc Cruft ===
  111. ====================
  112. - MAINTAINERS - presumably you're going to submit this code back to mainline
  113. and since you're the only one who cares about this arch (right now), you
  114. should add yourself to the toplevel MAINTAINERS file. do it.