• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2024 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
17import json
18import subprocess
19import sys
20
21
22def check_json_value(json_data):
23    for key, value in json_data.items():
24        if not isinstance(value, (bool, str, list)):
25            return False
26        if isinstance(value, list):
27            if not all(isinstance(item, str) for item in value):
28                return False
29    return True
30
31
32def check_json_content(json_data):
33    if len(json_data) == 1 and "kernelpermission" in json_data:
34        json_data = json_data["kernelpermission"]
35        return check_json_value(json_data)
36    else:
37        return False
38
39
40def check_json_file(file_path):
41    try:
42        with open(file_path, 'r') as file:
43            json_data = json.load(file)
44        if check_json_content(json_data):
45            return True
46        else:
47            print("kernel_permission.json is invalid")
48            return False
49    except FileNotFoundError:
50        print("kernel_permission.json not found")
51        return False
52    except json.JSONDecodeError:
53        print("kernel_permission.json doesn't conform to json specification")
54        return False
55    except Exception:
56        print("kernel_permission.json not found")
57        return False
58
59if __name__ == '__main__':
60    if len(sys.argv) != 2:
61        print("Usage: python script.py /path/to/library.so")
62        sys.exit(1)
63
64    so_path = sys.argv[1]
65
66    if (check_json_file("kernel_permission.json")):
67        command = ['llvm-objcopy', '--add-section', '.kernelpermission=kernel_permission.json', so_path]
68        result = subprocess.run(command, capture_output=True, text=True)
69
70        if result.returncode == 0:
71            print("llvm-objcopy executed successfully")
72        else:
73            print("llvm-objcopy failed")
74            print("Error:", result.stderr)
75