asm: use std::string_view in the assembler API

This commit is contained in:
lief-agent
2026-09-26 18:45:47 +02:00
committed by Romain Thomas
parent 04cc1cdad5
commit eea384f414
17 changed files with 34 additions and 27 deletions
+2 -1
View File
@@ -16,6 +16,7 @@
#include <sstream>
#include <nanobind/stl/string.h>
#include <nanobind/stl/string_view.h>
#include <nanobind/stl/vector.h>
#include <nanobind/stl/unique_ptr.h>
@@ -380,7 +381,7 @@ void create<Binary>(nb::module_& m) {
)
.def("assemble",
[] (Binary& self, uint64_t address, const std::string& Asm,
[] (Binary& self, uint64_t address, std::string_view Asm,
assembly::AssemblerConfig& config)
{
return nb::to_bytes(self.assemble(address, Asm, config));
+2 -2
View File
@@ -1,7 +1,7 @@
#include "LIEF/asm/AssemblerConfig.hpp"
#include "asm/pyAssembly.hpp"
#include <nanobind/stl/string.h>
#include <nanobind/stl/string_view.h>
#include <nanobind/trampoline.h>
#include <nanobind/stl/optional.h>
@@ -11,7 +11,7 @@ class PyAssemblerConfig : public assembly::AssemblerConfig {
public:
NB_TRAMPOLINE(assembly::AssemblerConfig);
std::optional<uint64_t> resolve_symbol(const std::string& name) override {
std::optional<uint64_t> resolve_symbol(std::string_view name) override {
NB_OVERRIDE(resolve_symbol, name);
}
+1
View File
@@ -7,6 +7,7 @@
#include "asm/pyAssembly.hpp"
#include <nanobind/stl/string.h>
#include <nanobind/stl/string_view.h>
#include <nanobind/stl/unique_ptr.h>
#include "nanobind/utils.hpp"
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <nanobind/stl/string.h>
#include <nanobind/stl/string_view.h>
#include "LIEF/runtime/assembler.hpp"
#include "LIEF/asm/AssemblerConfig.hpp"
@@ -23,7 +23,7 @@
namespace LIEF::runtime::py {
void init_assembler(nb::module_& m) {
m.def("assemble",
[] (uint64_t address, const std::string& Asm,
[] (uint64_t address, std::string_view Asm,
assembly::AssemblerConfig& config)
{
return nb::to_bytes(runtime::assemble(address, Asm, config));
+3 -2
View File
@@ -2,6 +2,7 @@
#include "LIEF/rust/asm/AssemblerConfig.hpp"
#include <string_view>
#include <memory>
class RustAssemblerConfig : public LIEF::assembly::AssemblerConfig {
@@ -10,8 +11,8 @@ class RustAssemblerConfig : public LIEF::assembly::AssemblerConfig {
LIEF::assembly::AssemblerConfig(),
impl_(const_cast<AssemblerConfig_r*>(&impl)) {}
std::optional<uint64_t> resolve_symbol(const std::string& name) override {
int64_t addr = impl_->resolve_symbol(name);
std::optional<uint64_t> resolve_symbol(std::string_view name) override {
int64_t addr = impl_->resolve_symbol(rust::Str(name.data(), name.size()));
if (addr < 0) {
return std::nullopt;
}
+3 -3
View File
@@ -43,7 +43,7 @@ void config_resolver() {
// lief-doc: config-resolver-start
class MyConfig : public LIEF::assembly::AssemblerConfig {
public:
std::optional<uint64_t> resolve_symbol(const std::string& name) override {
std::optional<uint64_t> resolve_symbol(std::string_view name) override {
if (name == "a_custom_function") {
return 0x1000;
}
@@ -72,8 +72,8 @@ void config_target() {
LIEF::assembly::AssemblerConfig(),
target_(&target) {}
std::optional<uint64_t> resolve_symbol(const std::string& name) override {
if (auto addr = target_->get_function_address(name)) {
std::optional<uint64_t> resolve_symbol(std::string_view name) override {
if (auto addr = target_->get_function_address(std::string(name))) {
return *addr;
}
return std::nullopt;
+1 -1
View File
@@ -19,7 +19,7 @@
```
```{eval-rst}
.. doxygenfunction:: LIEF::runtime::assemble(uint64_t, const std::string&, assembly::AssemblerConfig&)
.. doxygenfunction:: LIEF::runtime::assemble(uint64_t, std::string_view, assembly::AssemblerConfig&)
```
```{eval-rst}
+1 -1
View File
@@ -158,7 +158,7 @@ void memory_example() {
}
struct Config : LIEF::assembly::AssemblerConfig {
std::optional<uint64_t> resolve_symbol(const std::string& name) override {
std::optional<uint64_t> resolve_symbol(std::string_view name) override {
std::unique_ptr<LIEF::runtime::android::Module> libc =
LIEF::runtime::android::dlopen("libc.so");
if (libc == nullptr) {
+1 -1
View File
@@ -154,7 +154,7 @@ void memory_example() {
class Config : public LIEF::assembly::AssemblerConfig {
public:
std::optional<uint64_t> resolve_symbol(const std::string& name) override {
std::optional<uint64_t> resolve_symbol(std::string_view name) override {
// The message to print
static constexpr char HELLO[] = "Hello World\n";
static constexpr uint32_t HELLO_LEN = sizeof(HELLO) - 1;
+1 -1
View File
@@ -210,7 +210,7 @@ void memory_example() {
class Config : public LIEF::assembly::AssemblerConfig {
public:
std::optional<uint64_t> resolve_symbol(const std::string& name) override {
std::optional<uint64_t> resolve_symbol(std::string_view name) override {
// The message to print
static constexpr char HELLO[] = "Hello World\n";
static constexpr uint32_t HELLO_LEN = sizeof(HELLO) - 1;
+2 -1
View File
@@ -16,6 +16,7 @@
#ifndef LIEF_ABSTRACT_BINARY_H
#define LIEF_ABSTRACT_BINARY_H
#include <string_view>
#include <unordered_map>
#include <memory>
#include <vector>
@@ -384,7 +385,7 @@ class LIEF_API Binary : public Object {
///
/// If you need to configure the assembly engine or to define addresses for
/// symbols, you can provide your own assembly::AssemblerConfig.
std::vector<uint8_t> assemble(uint64_t address, const std::string& Asm,
std::vector<uint8_t> assemble(uint64_t address, std::string_view Asm,
assembly::AssemblerConfig& config =
assembly::AssemblerConfig::default_config());
+3 -3
View File
@@ -19,7 +19,7 @@
#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
namespace LIEF::assembly {
@@ -71,7 +71,7 @@ class LIEF_API AssemblerConfig {
/// ```cpp
/// class MyConfig : public AssemblerConfig {
/// public:
/// std::optional<uint64_t> resolve_symbol(const std::string& name) {
/// std::optional<uint64_t> resolve_symbol(std::string_view name) override {
/// if (name == "_my_function") {
/// return 0x4000;
/// }
@@ -79,7 +79,7 @@ class LIEF_API AssemblerConfig {
/// }
/// };
/// ```
virtual std::optional<uint64_t> resolve_symbol(const std::string& /*name*/) {
virtual std::optional<uint64_t> resolve_symbol(std::string_view /*name*/) {
return std::nullopt;
}
+3 -2
View File
@@ -21,6 +21,7 @@
#include "LIEF/asm/AssemblerConfig.hpp"
#include "LIEF/asm/Instruction.hpp"
#include <string_view>
#include <memory>
namespace LIEF {
@@ -59,11 +60,11 @@ class LIEF_API Engine {
}
std::vector<uint8_t>
assemble(uint64_t address, const std::string& Asm,
assemble(uint64_t address, std::string_view Asm,
AssemblerConfig& config = AssemblerConfig::default_config());
std::vector<uint8_t>
assemble(uint64_t address, const std::string& Asm, LIEF::Binary& bin,
assemble(uint64_t address, std::string_view Asm, LIEF::Binary& bin,
AssemblerConfig& config = AssemblerConfig::default_config());
std::vector<uint8_t> assemble(const llvm::MCInst& inst);
+2 -1
View File
@@ -19,6 +19,7 @@
#include "LIEF/iterators.hpp"
#include "LIEF/visibility.h"
#include <string_view>
#include <memory>
#include <ostream>
#include <string>
@@ -102,7 +103,7 @@ class LIEF_API Instruction {
const std::vector<uint8_t>& raw() const LIEF_LIFETIMEBOUND;
/// Instruction mnemonic (e.g. `br`)
std::string mnemonic() const;
std::string_view mnemonic() const LIEF_LIFETIMEBOUND;
/// Representation of the current instruction in a pretty assembly way
std::string to_string(bool with_address = true) const;
+2 -1
View File
@@ -16,6 +16,7 @@
#ifndef LIEF_RUNTIME_ASSEMBLER_H
#define LIEF_RUNTIME_ASSEMBLER_H
#include "LIEF/visibility.h"
#include <string_view>
#include <vector>
#include "LIEF/asm/AssemblerConfig.hpp"
@@ -44,7 +45,7 @@ namespace LIEF::runtime {
/// If you need to configure the assembly engine or to define addresses for
/// symbols, you can provide your own assembly::AssemblerConfig instance.
LIEF_API std::vector<uint8_t> assemble(
uint64_t addr, const std::string& Asm,
uint64_t addr, std::string_view Asm,
assembly::AssemblerConfig& config = assembly::AssemblerConfig::default_config()
);
+4 -4
View File
@@ -152,7 +152,7 @@ Binary::instructions_it Binary::disassemble(const uint8_t*, size_t,
}
std::vector<uint8_t> Binary::assemble(uint64_t /*address*/,
const std::string& /*Asm*/,
std::string_view /*Asm*/,
assembly::AssemblerConfig& /*config*/) {
LIEF_ERR(ASSEMBLY_NOT_SUPPORTED);
return {};
@@ -271,7 +271,7 @@ const std::vector<uint8_t>& Instruction::raw() const {
return empty;
}
std::string Instruction::mnemonic() const {
std::string_view Instruction::mnemonic() const {
return "";
}
@@ -361,13 +361,13 @@ Engine::instructions_it Engine::disassemble(const uint8_t*, size_t, uint64_t) {
}
std::vector<uint8_t> Engine::assemble(uint64_t /*address*/,
const std::string& /*Asm*/,
std::string_view /*Asm*/,
AssemblerConfig& /*config*/) {
return {};
}
std::vector<uint8_t> Engine::assemble(uint64_t /*address*/,
const std::string& /*Asm*/,
std::string_view /*Asm*/,
LIEF::Binary& /*bin*/,
AssemblerConfig& /*config*/) {
return {};
+1 -1
View File
@@ -6,7 +6,7 @@
#include "messages.hpp"
namespace LIEF::runtime {
std::vector<uint8_t> assemble(uint64_t /*addr*/, const std::string& /*Asm*/,
std::vector<uint8_t> assemble(uint64_t /*addr*/, std::string_view /*Asm*/,
assembly::AssemblerConfig& /*config*/) {
LIEF_ERR(ASSEMBLY_NOT_SUPPORTED);
return {};