Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with memory.x in the project root #22

Merged
merged 3 commits into from
Feb 5, 2021
Merged

Problem with memory.x in the project root #22

merged 3 commits into from
Feb 5, 2021

Conversation

DerFetzer
Copy link
Contributor

@DerFetzer DerFetzer commented Feb 4, 2021

When the linker script with RAM location is in the root directory where you invoke cargo it is always used by the linker.
So only the beginning of the stack is changed which leads to an invalid memory layout.

When you look at the man page for GNU ld it says for the -T option:

If scriptfile does not exist in the current directory, "ld" looks for it in the directories specified by any preceding -L options.

So it seems that this is also valid for included scripts.

As a first attempt to fix this problem I set the working dir for the second linker invocation to the temp dir containing the modified script. This works for my setup where I have a memory.x in the project root as well as for the example app in this repository.

It could be a problem though when you have multiple linker scripts in project root. But maybe you could set the project root as additional -L directory. What do you think?

@japaric japaric self-assigned this Feb 5, 2021
@japaric
Copy link
Member

japaric commented Feb 5, 2021

Thanks for the PR! The solution looks good to me.

It could be a problem though when you have multiple linker scripts in project root. But maybe you could set the project root as additional -L directory. What do you think?

Yeah, I tested this out with your changes and these linker scripts in the root of the project:

$ cat memory.x
MEMORY
{
  FLASH : ORIGIN = 0x08000000, LENGTH = 256K
  RAM : ORIGIN = 0x20000000, LENGTH = 64K
}

INCLUDE empty.x

$ touch empty.x

$ cargo b
(..)
= note: rust-lld: error: memory.x:7: cannot find linker script empty.x

The linker fails to find linker scripts that weren't modified by flip-link so we should include the project root in the search path via a -L flag, I think.

src/main.rs Outdated
// HACK `-L` needs to go after `-flavor gnu`; position is currently hardcoded
args.insert(2, "-L".to_string());
args.insert(3, tempdir.path().display().to_string());
// we also need to override `_stack_start` to make the stack start below fake RAM
args.insert(3, current_dir.to_str().unwrap_or(".").to_string());
Copy link
Member

Choose a reason for hiding this comment

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

Taking a closer look at the surrounding code, I think we should use Command::arg instead of pushing arguments into a Vec<String> because Command::arg will accept a Path directly. That way we can about the string conversion which may be lossy. Could you please do that change as part of this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. I was not sure myself about that part. I'll have a look later.

@japaric
Copy link
Member

japaric commented Feb 5, 2021

Awesome, thanks again!

@japaric japaric merged commit df93cb1 into knurling-rs:main Feb 5, 2021
@DerFetzer
Copy link
Contributor Author

You are welcome!

@Urhengulas Urhengulas mentioned this pull request Jun 16, 2021
bors bot added a commit that referenced this pull request Jul 7, 2021
42: Tests for 22 r=Urhengulas a=Urhengulas

Also partially depends on #40 and fixes #26, by implementing tests for #22.

Co-authored-by: Johann Hemmann <johann.hemmann@code.berlin>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants