/src/rtstartup/rsbegin.rs

https://gitlab.com/alx741/rust · Rust · 78 lines · 30 code · 10 blank · 38 comment · 0 complexity · 0a24e0add96c043c5a64d9f983dfd74d MD5 · raw file

  1. // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // http://rust-lang.org/COPYRIGHT.
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. // rsbegin.o and rsend.o are the so called "compiler runtime startup objects".
  11. // They contain code needed to correctly initialize the compiler runtime.
  12. //
  13. // When an executable or dylib image is linked, all user code and libraries are
  14. // "sandwiched" between these two object files, so code or data from rsbegin.o
  15. // become first in the respective sections of the image, whereas code and data
  16. // from rsend.o become the last ones. This effect can be used to place symbols
  17. // at the beginning or at the end of a section, as well as to insert any required
  18. // headers or footers.
  19. //
  20. // Note that the actual module entry point is located in the C runtime startup
  21. // object (usually called `crtX.o), which then invokes initialization callbacks
  22. // of other runtime components (registered via yet another special image section).
  23. #![crate_type="rlib"]
  24. #![no_std]
  25. #![allow(non_camel_case_types)]
  26. #[cfg(all(target_os="windows", target_arch = "x86", target_env="gnu"))]
  27. pub mod eh_frames
  28. {
  29. #[no_mangle]
  30. #[link_section = ".eh_frame"]
  31. // Marks beginning of the stack frame unwind info section
  32. pub static __EH_FRAME_BEGIN__: [u8; 0] = [];
  33. // Scratch space for unwinder's internal book-keeping.
  34. // This is defined as `struct object` in $GCC/libgcc/unwind-dw2-fde.h.
  35. static mut obj: [isize; 6] = [0; 6];
  36. // Unwind info registration/deregistration routines.
  37. // See the docs of `unwind` module in libstd.
  38. extern {
  39. fn rust_eh_register_frames(eh_frame_begin: *const u8, object: *mut u8);
  40. fn rust_eh_unregister_frames(eh_frame_begin: *const u8, object: *mut u8);
  41. }
  42. unsafe fn init() {
  43. // register unwind info on module startup
  44. rust_eh_register_frames(&__EH_FRAME_BEGIN__ as *const u8,
  45. &mut obj as *mut _ as *mut u8);
  46. }
  47. unsafe fn uninit() {
  48. // unregister on shutdown
  49. rust_eh_unregister_frames(&__EH_FRAME_BEGIN__ as *const u8,
  50. &mut obj as *mut _ as *mut u8);
  51. }
  52. // MSVC-specific init/uninit routine registration
  53. pub mod ms_init
  54. {
  55. // .CRT$X?? sections are roughly analogous to ELF's .init_array and .fini_array,
  56. // except that they exploit the fact that linker will sort them alphabitically,
  57. // so e.g. sections with names between .CRT$XIA and .CRT$XIZ are guaranteed to be
  58. // placed between those two, without requiring any ordering of objects on the linker
  59. // command line.
  60. // Note that ordering of same-named sections from different objects is not guaranteed.
  61. // Since .CRT$XIA contains init array's header symbol, which must always come first,
  62. // we place our initialization callback into .CRT$XIB.
  63. #[link_section = ".CRT$XIB"] // .CRT$XI? : C initialization callbacks
  64. pub static P_INIT: unsafe fn() = super::init;
  65. #[link_section = ".CRT$XTY"] // .CRT$XT? : C termination callbacks
  66. pub static P_UNINIT: unsafe fn() = super::uninit;
  67. }
  68. }