Don't use objcopy to embed Android icudtl (flutter/engine#46862)

When objcopy is used to embed data into a linkable object file, that
object file will only have default bits set in its header for ABI etc..
If the linker doesn't cooperate by ignoring ABI mismatches on object
files without code, then linking will fail. This PR stops using objcopy
to create an object file that embeds icudtl.dat into the Android
embedder, and instead uses the `bin_to_assembly.py` script that we're
already using for Dart VM snapshot data.

Context in https://github.com/llvm/llvm-project/issues/68915
This commit is contained in:
Zachary Anderson
2023-10-13 07:33:24 -07:00
committed by GitHub
parent a6abdb4565
commit 7922a335e8
7 changed files with 121 additions and 284 deletions

View File

@@ -0,0 +1,103 @@
# Copyright 2013 The Flutter Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Generates an assembly file defining a given symbol with the bytes from a
# binary file. Places the symbol in a text section if 'executable' is true,
# otherwise places the symbol in a read-only data section.
template("bin_to_assembly") {
assert(defined(invoker.deps), "Must define deps")
assert(defined(invoker.input), "Must define input binary file")
assert(defined(invoker.symbol), "Must define symbol name")
assert(defined(invoker.executable), "Must define boolean executable")
action(target_name) {
deps = invoker.deps
script = "//third_party/dart/runtime/tools/bin_to_assembly.py"
output = "$target_gen_dir/${invoker.input}.S"
args = [
"--input",
rebase_path(invoker.input),
"--output",
rebase_path(output),
"--symbol_name",
invoker.symbol,
"--target_os",
current_os,
]
if (defined(invoker.size_symbol)) {
args += [
"--size_symbol_name",
invoker.size_symbol,
"--target_arch",
current_cpu,
]
}
if (invoker.executable) {
args += [ "--executable" ]
}
if (current_os != "win") {
args += [ "--incbin" ]
}
inputs = [
script,
invoker.input,
]
outputs = [ output ]
}
}
# Generates an object file defining a given symbol with the bytes from a
# binary file. Places the symbol in the read-only data section.
template("bin_to_coff") {
assert(defined(invoker.deps), "Must define deps")
assert(defined(invoker.input), "Must define input binary file")
assert(defined(invoker.symbol), "Must define symbol name")
assert(defined(invoker.executable), "Must define executable")
action(target_name) {
deps = invoker.deps
script = "//third_party/dart/runtime/tools/bin_to_coff.py"
output = "$target_gen_dir/${invoker.input}.o"
args = [
"--input",
rebase_path(invoker.input),
"--output",
rebase_path(output),
"--symbol_name",
invoker.symbol,
]
if (defined(invoker.size_symbol)) {
args += [
"--size_symbol_name",
invoker.size_symbol,
]
}
if (invoker.executable) {
args += [ "--executable" ]
}
args += [ "--arch=$current_cpu" ]
inputs = [ invoker.input ]
outputs = [ output ]
}
}
# Generates a linkable output file defining the specified symbol with the bytes
# from the binary file. Emits a COFF object file when targeting Windows,
# otherwise assembly.
template("bin_to_linkable") {
assert(defined(invoker.deps), "Must define deps")
assert(defined(invoker.input), "Must define input binary file")
assert(defined(invoker.symbol), "Must define symbol name")
target_type = "bin_to_assembly"
if (is_win) {
target_type = "bin_to_coff"
}
target(target_type, target_name) {
forward_variables_from(invoker, "*")
}
}

View File

@@ -388,7 +388,6 @@
../../../flutter/sky/tools/create_xcframework.py
../../../flutter/sky/tools/dist_dart_pkg.py
../../../flutter/sky/tools/install_framework_headers.py
../../../flutter/sky/tools/objcopy.py
../../../flutter/testing
../../../flutter/third_party/.clang-tidy
../../../flutter/third_party/.gitignore

View File

@@ -4,6 +4,7 @@
import("//build/compiled_action.gni")
import("//build/fuchsia/sdk.gni")
import("//flutter/build/bin_to_obj.gni")
import("//flutter/common/config.gni")
import("//flutter/impeller/tools/impeller.gni")
import("//flutter/lib/ui/dart_ui.gni")
@@ -110,103 +111,6 @@ compiled_action("generate_snapshot_bin") {
}
}
# Generates an assembly file defining a given symbol with the bytes from a
# binary file. Places the symbol in a text section if 'executable' is true,
# otherwise places the symbol in a read-only data section.
template("bin_to_assembly") {
assert(defined(invoker.deps), "Must define deps")
assert(defined(invoker.input), "Must define input binary file")
assert(defined(invoker.symbol), "Must define symbol name")
assert(defined(invoker.executable), "Must define boolean executable")
action(target_name) {
deps = invoker.deps
script = "//third_party/dart/runtime/tools/bin_to_assembly.py"
output = invoker.input + ".S"
args = [
"--input",
rebase_path(invoker.input),
"--output",
rebase_path(output),
"--symbol_name",
invoker.symbol,
"--target_os",
current_os,
]
if (defined(invoker.size_symbol)) {
args += [
"--size_symbol_name",
invoker.size_symbol,
"--target_arch",
current_cpu,
]
}
if (invoker.executable) {
args += [ "--executable" ]
}
inputs = [
script,
invoker.input,
]
outputs = [ output ]
}
}
# Generates an object file defining a given symbol with the bytes from a
# binary file. Places the symbol in the read-only data section.
template("bin_to_coff") {
assert(defined(invoker.deps), "Must define deps")
assert(defined(invoker.input), "Must define input binary file")
assert(defined(invoker.symbol), "Must define symbol name")
assert(defined(invoker.executable), "Must define executable")
action(target_name) {
deps = invoker.deps
script = "//third_party/dart/runtime/tools/bin_to_coff.py"
output = invoker.input + ".o"
args = [
"--input",
rebase_path(invoker.input),
"--output",
rebase_path(output),
"--symbol_name",
invoker.symbol,
]
if (defined(invoker.size_symbol)) {
args += [
"--size_symbol_name",
invoker.size_symbol,
]
}
if (invoker.executable) {
args += [ "--executable" ]
}
args += [ "--arch=$current_cpu" ]
inputs = [ invoker.input ]
outputs = [ output ]
}
}
# Generates a linkable output file defining the specified symbol with the bytes
# from the binary file. Emits a COFF object file when targeting Windows,
# otherwise assembly.
template("bin_to_linkable") {
assert(defined(invoker.deps), "Must define deps")
assert(defined(invoker.input), "Must define input binary file")
assert(defined(invoker.symbol), "Must define symbol name")
target_type = "bin_to_assembly"
if (is_win) {
target_type = "bin_to_coff"
}
target(target_type, target_name) {
forward_variables_from(invoker, "*")
}
}
bin_to_linkable("vm_snapshot_data_linkable") {
deps = [ ":generate_snapshot_bin" ]
input = "$target_gen_dir/vm_isolate_snapshot.bin"

View File

@@ -78,12 +78,11 @@ static const std::string kAllowedDartFlags[] = {
// of the engine's own symbols on some older versions of Android.
#if FML_OS_ANDROID
extern uint8_t _binary_icudtl_dat_start[];
extern uint8_t _binary_icudtl_dat_end[];
extern size_t _binary_icudtl_dat_size;
static std::unique_ptr<fml::Mapping> GetICUStaticMapping() {
return std::make_unique<fml::NonOwnedMapping>(
_binary_icudtl_dat_start,
_binary_icudtl_dat_end - _binary_icudtl_dat_start);
return std::make_unique<fml::NonOwnedMapping>(_binary_icudtl_dat_start,
_binary_icudtl_dat_size);
}
#endif

View File

@@ -4,6 +4,7 @@
import("//build/config/android/config.gni")
import("//build/toolchain/clang.gni")
import("//flutter/build/bin_to_obj.gni")
import("//flutter/build/zip_bundle.gni")
import("//flutter/common/config.gni")
import("//flutter/impeller/tools/impeller.gni")
@@ -63,11 +64,18 @@ shared_library("flutter_shell_native") {
ldflags = [ "-Wl,--version-script=" + rebase_path("android_exports.lst") ]
}
bin_to_assembly("icudtl_asm") {
deps = []
input = "//third_party/icu/flutter/icudtl.dat"
symbol = "_binary_icudtl_dat_start"
size_symbol = "_binary_icudtl_dat_size"
executable = false
}
source_set("flutter_shell_native_src") {
visibility = [ ":*" ]
sources = [
"$root_build_dir/flutter_icu/icudtl.o",
"android_choreographer.cc",
"android_choreographer.h",
"android_context_gl_impeller.cc",
@@ -121,9 +129,11 @@ source_set("flutter_shell_native_src") {
"vsync_waiter_android.h",
]
sources += get_target_outputs(":icudtl_asm")
public_deps = [
":android_gpu_configuration",
":icudtl_object",
":icudtl_asm",
":image_generator",
"//flutter/assets",
"//flutter/common",
@@ -424,28 +434,6 @@ action("flutter_shell_java") {
]
}
action("icudtl_object") {
script = "//flutter/sky/tools/objcopy.py"
icudtl_input = "//third_party/icu/flutter/icudtl.dat"
icudtl_output = "$root_build_dir/flutter_icu/icudtl.o"
inputs = [ "$icudtl_input" ]
outputs = [ "$icudtl_output" ]
args = [
"--objcopy",
rebase_path(android_objcopy),
"--input",
rebase_path(icudtl_input),
"--output",
rebase_path(icudtl_output),
"--arch",
current_cpu,
]
}
action("android_jar") {
script = "//build/android/gyp/create_flutter_jar.py"

View File

@@ -1,58 +0,0 @@
#!/usr/bin/env python3
#
# Copyright 2013 The Flutter Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import argparse
import os
import subprocess
import sys
# BFD architecture names recognized by objcopy.
BFD_ARCH = {
'arm': 'arm',
'arm64': 'aarch64',
'x86': 'i386',
'x64': 'i386:x86-64',
}
# BFD target names recognized by objcopy.
BFD_TARGET = {
'arm': 'elf32-littlearm',
'arm64': 'elf64-littleaarch64',
'x86': 'elf32-i386',
'x64': 'elf64-x86-64',
}
def main():
parser = argparse.ArgumentParser(
description='Convert a data file to an object file'
)
parser.add_argument('--objcopy', type=str, required=True)
parser.add_argument('--input', type=str, required=True)
parser.add_argument('--output', type=str, required=True)
parser.add_argument('--arch', type=str, required=True)
args = parser.parse_args()
input_dir, input_file = os.path.split(args.input)
output_path = os.path.abspath(args.output)
subprocess.check_call([
args.objcopy,
'-I',
'binary',
'-O',
BFD_TARGET[args.arch],
'-B',
BFD_ARCH[args.arch],
input_file,
output_path,
],
cwd=input_dir)
if __name__ == '__main__':
sys.exit(main())

View File

@@ -135,106 +135,8 @@ int _checkAndroid(String outPath, String nmPath, Iterable<String> builds) {
};
final Map<String, String> expectedSymbols = <String, String>{
'JNI_OnLoad': 'T',
'_binary_icudtl_dat_size': 'A',
'_binary_icudtl_dat_start': 'D',
// TODO(dnfield): Remove these once Clang lld does not expose them.
// arm https://bugs.fuchsia.dev/p/fuchsia/issues/detail?id=47943
'__adddf3': 'T',
'__addsf3': 'T',
'__aeabi_cdcmpeq': 'T',
'__aeabi_cdcmple': 'T',
'__aeabi_cdrcmple': 'T',
'__aeabi_cfcmpeq': 'T',
'__aeabi_cfcmple': 'T',
'__aeabi_cfrcmple': 'T',
'__aeabi_d2lz': 'T',
'__aeabi_d2uiz': 'T',
'__aeabi_d2ulz': 'T',
'__aeabi_dadd': 'T',
'__aeabi_dcmpeq': 'T',
'__aeabi_dcmpge': 'T',
'__aeabi_dcmpgt': 'T',
'__aeabi_dcmple': 'T',
'__aeabi_dcmplt': 'T',
'__aeabi_ddiv': 'T',
'__aeabi_dmul': 'T',
'__aeabi_drsub': 'T',
'__aeabi_dsub': 'T',
'__aeabi_f2d': 'T',
'__aeabi_f2lz': 'T',
'__aeabi_f2ulz': 'T',
'__aeabi_fadd': 'T',
'__aeabi_fcmpeq': 'T',
'__aeabi_fcmpge': 'T',
'__aeabi_fcmpgt': 'T',
'__aeabi_fcmple': 'T',
'__aeabi_fcmplt': 'T',
'__aeabi_frsub': 'T',
'__aeabi_fsub': 'T',
'__aeabi_i2d': 'T',
'__aeabi_i2f': 'T',
'__aeabi_l2d': 'T',
'__aeabi_l2f': 'T',
'__aeabi_lasr': 'T',
'__aeabi_ldivmod': 'T',
'__aeabi_llsl': 'T',
'__aeabi_llsr': 'T',
'__aeabi_ui2d': 'T',
'__aeabi_ui2f': 'T',
'__aeabi_uidiv': 'T',
'__aeabi_uidivmod': 'T',
'__aeabi_ul2d': 'T',
'__aeabi_ul2f': 'T',
'__aeabi_uldivmod': 'T',
'__ashldi3': 'T',
'__ashrdi3': 'T',
'__cmpdf2': 'T',
'__cmpsf2': 'T',
'__divdf3': 'T',
'__divdi3': 'T',
'__eqdf2': 'T',
'__eqsf2': 'T',
'__extendsfdf2': 'T',
'__fixdfdi': 'T',
'__fixsfdi': 'T',
'__fixunsdfdi': 'T',
'__fixunsdfsi': 'T',
'__fixunssfdi': 'T',
'__floatdidf': 'T',
'__floatdisf': 'T',
'__floatsidf': 'T',
'__floatsisf': 'T',
'__floatundidf': 'T',
'__floatundisf': 'T',
'__floatunsidf': 'T',
'__floatunsisf': 'T',
'__gedf2': 'T',
'__gesf2': 'T',
'__gnu_ldivmod_helper': 'T',
'__gnu_uldivmod_helper': 'T',
'__gtdf2': 'T',
'__gtsf2': 'T',
'__ledf2': 'T',
'__lesf2': 'T',
'__lshrdi3': 'T',
'__ltdf2': 'T',
'__ltsf2': 'T',
'__muldf3': 'T',
'__nedf2': 'T',
'__nesf2': 'T',
'__subdf3': 'T',
'__subsf3': 'T',
'__udivdi3': 'T',
'__udivsi3': 'T',
// arm64
'__clz_tab': 'R',
'__udivti3': 'T',
// arm64 && x64
'__emutls_get_address': 'T',
'__emutls_register_common': 'T',
// jit x86
'__moddi3': 'T',
'__umoddi3': 'T',
'_binary_icudtl_dat_size': 'R',
'_binary_icudtl_dat_start': 'R',
};
final Map<String, String> badSymbols = <String, String>{};
for (final String key in entryMap.keys) {