1 /*
   2  * Copyright (c) 2019, Red Hat, Inc.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.tools.jlink.internal.plugins;
  27 
  28 import java.util.Map;
  29 
  30 import jdk.tools.jlink.internal.PluginRepository;
  31 import jdk.tools.jlink.internal.ResourcePoolManager;
  32 import jdk.tools.jlink.internal.ResourcePoolManager.ResourcePoolImpl;
  33 import jdk.tools.jlink.plugin.Plugin;
  34 import jdk.tools.jlink.plugin.ResourcePool;
  35 import jdk.tools.jlink.plugin.ResourcePoolBuilder;
  36 
  37 /**
  38  * Combined debug stripping plugin: Java debug attributes and native debug
  39  * symbols.
  40  *
  41  * @author Severin Gehwolf
  42  */
  43 public final class DefaultStripDebugPlugin implements Plugin {
  44 
  45     public static final String NAME = "strip-debug";
  46     private static final String STRIP_NATIVE_DEBUG_PLUGIN = "strip-native-debug-symbols";
  47     private static final String OMIT_DEBUGINFO = "omit-debuginfo";
  48 
  49     private final Plugin javaStripPlugin;
  50     private final NativePluginFactory stripNativePluginFactory;
  51 
  52     public DefaultStripDebugPlugin() {
  53         this(new StripJavaDebugAttributesPlugin(),
  54              new DefaultNativePluginFactory());
  55     }
  56 
  57     public DefaultStripDebugPlugin(Plugin javaStripPlugin,
  58                                    NativePluginFactory nativeStripPluginFact) {
  59         this.javaStripPlugin = javaStripPlugin;
  60         this.stripNativePluginFactory = nativeStripPluginFact;
  61     }
  62 
  63     @Override
  64     public String getName() {
  65         return NAME;
  66     }
  67 
  68     @Override
  69     public String getDescription() {
  70         return PluginsResourceBundle.getDescription(NAME);
  71     }
  72 
  73     @Override
  74     public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
  75         Plugin stripNativePlugin = stripNativePluginFactory.create();
  76         if (stripNativePlugin != null) {
  77             Map<String, String> stripNativeConfig = Map.<String, String>of(
  78                                      STRIP_NATIVE_DEBUG_PLUGIN, OMIT_DEBUGINFO);
  79             stripNativePlugin.configure(stripNativeConfig);
  80             ResourcePoolManager outRes =
  81                                  new ResourcePoolManager(in.byteOrder(),
  82                                                         ((ResourcePoolImpl)in).getStringTable());
  83             ResourcePool strippedJava = javaStripPlugin.transform(in,
  84                                                                   outRes.resourcePoolBuilder());
  85             return stripNativePlugin.transform(strippedJava, out);
  86         } else {
  87             return javaStripPlugin.transform(in, out);
  88         }
  89     }
  90 
  91     public interface NativePluginFactory {
  92         Plugin create();
  93     }
  94 
  95     private static class DefaultNativePluginFactory implements NativePluginFactory {
  96 
  97         @Override
  98         public Plugin create() {
  99             return PluginRepository.getPlugin(STRIP_NATIVE_DEBUG_PLUGIN,
 100                                               ModuleLayer.boot());
 101         }
 102 
 103     }
 104 
 105 }