Skip to content

Commit

Permalink
__DATA -> __DATA_CONST
Browse files Browse the repository at this point in the history
Fixes #1
  • Loading branch information
mitsuhiko committed Jul 2, 2023
1 parent 7973f0b commit 8f0e3b4
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "small_ctor"
version = "0.1.0"
version = "0.1.1"
edition = "2018"
authors = ["Armin Ronacher <armin.ronacher@active-4.com>"]
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ pub fn ctor(args: TokenStream, input: TokenStream) -> TokenStream {
TokenTree::Punct(Punct::new(',', Spacing::Alone)),
TokenTree::Ident(Ident::new("link_section", Span::call_site())),
TokenTree::Punct(Punct::new('=', Spacing::Alone)),
TokenTree::Literal(Literal::string("__DATA,__mod_init_func")),
TokenTree::Literal(Literal::string("__DATA_CONST,__mod_init_func")),
],
))
],
Expand Down

4 comments on commit 8f0e3b4

@danakj
Copy link

@danakj danakj commented on 8f0e3b4 Jul 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirm that this change is working in Chromium, where we're linking the rust rlibs with C++ with lld.

It appears that this did not work in the tests you have in this repo though? Does this perhaps need to be configurable depending on the linker choice?

I wonder if the tests there would pass with -Clinker=clang or -Clinker=clang -Clink-arg=-fuse-ld=lld

@danakj
Copy link

@danakj danakj commented on 8f0e3b4 Jul 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What relocation model is used when compiling the tests?
https://doc.rust-lang.org/rustc/codegen-options/index.html#relocation-model

I see that when relocation model is static for MachO, LLVM expects to put them in a different place:

void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
                                               const TargetMachine &TM) {
  TargetLoweringObjectFile::Initialize(Ctx, TM);
  if (TM.getRelocationModel() == Reloc::Static) {
    StaticCtorSection = Ctx.getMachOSection("__TEXT", "__constructor", 0,
                                            SectionKind::getData());
    StaticDtorSection = Ctx.getMachOSection("__TEXT", "__destructor", 0,
                                            SectionKind::getData());
  } else {
    StaticCtorSection = Ctx.getMachOSection("__DATA", "__mod_init_func",
                                            MachO::S_MOD_INIT_FUNC_POINTERS,
                                            SectionKind::getData());
    StaticDtorSection = Ctx.getMachOSection("__DATA", "__mod_term_func",
                                            MachO::S_MOD_TERM_FUNC_POINTERS,
                                            SectionKind::getData());
  }

https://github.com/llvm/llvm-project/blob/4b3eaee2701afa2549c9cd0f78692598e9bfd44e/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp#L1191-L1203

FWIW, looking at otool I can see that clang puts C++ static initializers in __DATA_CONST,__mod_term_func, which is not strictly what the code above says, so that must end up modified somehow.

@danakj
Copy link

@danakj danakj commented on 8f0e3b4 Jul 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mitsuhiko
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will look into this now. For some reason when I ran this locally on my machine it passed but apparently it's now failing there too, even for the most basic test of compiling and running the example.

Please sign in to comment.