#!/bin/bash

# enable bash debugging
KMDBSCRIPT="${KMDBSCRIPT:=0}"
if [ $KMDBSCRIPT == "1" ]
then
  set -x
fi

# check command line arguments
if [ "$#" -ne 3 ]; then
  echo "Usage: $0 input_object output_kernel elf_section_name" >&2
  exit 1
fi

if [ ! -f "$1" ]; then
  echo "input object $1 is not valid" >&2
  exit 1
fi

if [[ "x86_64" == "x86_64" ]]; then
  objcopy -B i386:x86-64 -I binary -O elf64-x86-64 --rename-section .data="$3" --weaken "$1" "$2"
elif [[ "x86_64" == "aarch64" ]]; then
  objcopy -B aarch64 -I binary -O elf64-littleaarch64 --rename-section .data="$3" --weaken "$1" "$2"
elif [[ "x86_64" == "ppc64" ]]; then
  objcopy -B ppc64 -I binary -O elf64-powerpc --rename-section .data="$3" --weaken "$1" "$2"
elif [[ "x86_64" == "ppc64le" ]]; then
  objcopy -B powerpc -I binary -O elf64-powerpcle --rename-section .data="$3" --weaken "$1" "$2"
fi
