startup.hss 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. [Main]
  2. Title=Startup Sections
  3. Index=_st..., _stl...
  4. [Top]
  5. The concept of <U>startup sections</U> is unique to the TIGCC linker. It only
  6. makes sense in low-resource environments like calculators. The idea is that a
  7. file that is imported should be able to specify that it needs certain code to
  8. be executed at the beginning of the program. Usually, there are two
  9. approaches to address this situation: constructors and main function
  10. wrappers.
  11. <BR><BR>
  12. If constructors are used to handle this, a lot of memory is wasted: A
  13. constructor table needs to be created with appropriate code to handle its
  14. contents; every item needs to save all registers except a few, and sharing
  15. data between two constructors requires global variables. Moreover, using
  16. constructors, it is not possible to specify the order in which startup code
  17. is to be called; however, parts of the startup code often need to rely on
  18. other parts to be executed first.
  19. <BR><BR>
  20. Main function wrappers appear in almost every environment. But since these
  21. wrappers are fixed, they need to handle all startup code that might possibly
  22. be needed, instead of letting each file choose its own startup code. For
  23. example, such fixed startup code would need to handle exceptions even if the
  24. program never generates them, or fill certain global variables that are never
  25. read.
  26. <BR><BR>
  27. Startup sections are actually a wrapper around the main function, but they
  28. can achieve even more flexibility than constructors: They are numbered and
  29. executed in the exact order specified by the numbers, and no extra code is
  30. executed between two consecutive startup sections, so registers can be used
  31. to pass data between two sections.
  32. <BR><BR>
  33. Startup sections can be used not only to insert code at the beginning of the
  34. program, but also to generate the required headers for certain file formats.
  35. Sometimes this is easier than writing the linker code to insert the required
  36. headers (see <A HREF="$$LINK(formats)">TIGCC Linker File Formats</A>).
  37. <BR><BR>
  38. Since libraries may need to contain a header, some stub code that is called
  39. when the user tries to execute the library, and possibly some startup code,
  40. they may also have startup sections. However, it does not really make sense
  41. to include a startup section designed for a program in a library. Therefore,
  42. there are library startup sections, which may appear in both libraries and
  43. programs, and program startup sections, which may appear only in programs.
  44. Library startup sections are always included <I>before</I> program startup
  45. sections.
  46. <BR><BR>
  47. Startup sections are detected based on their name. To declare a program
  48. startup section, name the section <CODE>_st<I>n</I></CODE>, where <I>n</I> is
  49. a value from 1 to 99999 (higher values for <I>n</I> may be accepted if the
  50. object file format supports section names longer than 8 characters, but it is
  51. not recommended to use them). To declare a library startup section, name it
  52. <CODE>_stl<I>n</I></CODE>, where <I>n</I> is a value from 1 to 9999 (higher
  53. values are not permitted). Startup sections are included in ascending order;
  54. if two startup sections use the same index, their order is undefined.