Android Studio 64 Bit Inline Arm Assembly
Solution 1:
As a general rule, you want to move as much of the code out of the inline asm as possible. In this case for instance, you are moving #180 to x8 to indicate the service number to invoke. But what if you want to open 2 files in a row, passing 180 in x8 both times? Ideally you'd want to avoid setting the identical value into the same register twice if you could, right? But by putting the mov
inside the asm, you are forcing the value to be set every time.
You can avoid that by moving the assignments out of the asm. While I don't have an arm64 platform to run this on, the output from godbolt looks right:
std::string system_file = "/system/bin/sh";
constchar *ptr = system_file.c_str();
register std::int64_t file_descriptor asm("x0");
register std::int64_t x8 asm("x8")= 180;
register std::int64_t x1 asm("x1")= 0;
__asm__ volatile("svc #1"
:"=r"(file_descriptor)
:"0"(ptr),"r"(x1),"r"(x8)
:"memory"
);
Since x1 and x8 are listed as inputs (ie after the 2nd colon), gcc assumes their value is unchanged. And since the assignments took place in C code, it knows what values are there too.
The "0" might look a bit odd, but it's saying that ptr
will be in the same place as parameter #0 (ie file_descriptor, which uses asm("x0")
). So the input will be ptr, the output will be file_descriptor, and both use the same register.
I'll also mention that all the svc
samples I've seen use svc #0
.
Post a Comment for "Android Studio 64 Bit Inline Arm Assembly"